Skip to content

Commit 93626d8

Browse files
committed
drastically improve performance using requestAnimationFrame and dirtyChecking
instead of calling on each event the callback, it calls max on each frame the callback. Also set the width of sensors to extreme high value instead of read expensive dimensions from parent. Compared to the <object> sensor this variant is faster in each frame and way faster in the initialization. Fixes marcj#85
1 parent 4dba9c4 commit 93626d8

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/ResizeSensor.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
;
77
(function() {
88

9+
var requestAnimationFrame = window.requestAnimationFrame ||
10+
window.mozRequestAnimationFrame ||
11+
window.webkitRequestAnimationFrame ||
12+
function (fn) {
13+
return window.setTimeout(fn, 20);
14+
};
15+
916
/**
1017
* Class for dimension change detection.
1118
*
@@ -77,34 +84,51 @@
7784
'</div>';
7885
element.appendChild(element.resizeSensor);
7986

80-
if (!{fixed: 1, absolute: 1}[getComputedStyle(element, 'position')]) {
87+
if (getComputedStyle(element, 'position') == 'static') {
8188
element.style.position = 'relative';
8289
}
8390

8491
var expand = element.resizeSensor.childNodes[0];
8592
var expandChild = expand.childNodes[0];
8693
var shrink = element.resizeSensor.childNodes[1];
87-
var shrinkChild = shrink.childNodes[0];
88-
89-
var lastWidth, lastHeight;
9094

9195
var reset = function() {
92-
expandChild.style.width = expand.offsetWidth + 10 + 'px';
93-
expandChild.style.height = expand.offsetHeight + 10 + 'px';
94-
expand.scrollLeft = expand.scrollWidth;
95-
expand.scrollTop = expand.scrollHeight;
96-
shrink.scrollLeft = shrink.scrollWidth;
97-
shrink.scrollTop = shrink.scrollHeight;
98-
lastWidth = element.offsetWidth;
99-
lastHeight = element.offsetHeight;
96+
expandChild.style.width = 100000 + 'px';
97+
expandChild.style.height = 100000 + 'px';
98+
99+
expand.scrollLeft = 100000;
100+
expand.scrollTop = 100000;
101+
102+
shrink.scrollLeft = 100000;
103+
shrink.scrollTop = 100000;
100104
};
101105

102106
reset();
107+
var dirty = false;
103108

104-
var changed = function() {
105-
if (element.resizedAttached) {
109+
var dirtyChecking = function() {
110+
if (!element.resizedAttached) return;
111+
112+
if (dirty) {
106113
element.resizedAttached.call();
114+
dirty = false;
107115
}
116+
117+
requestAnimationFrame(dirtyChecking);
118+
};
119+
120+
requestAnimationFrame(dirtyChecking);
121+
var lastWidth, lastHeight;
122+
var cachedWidth, cachedHeight; //useful to not query offsetWidth twice
123+
124+
var onScroll = function() {
125+
if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) {
126+
dirty = true;
127+
128+
lastWidth = cachedWidth;
129+
lastHeight = cachedHeight;
130+
}
131+
reset();
108132
};
109133

110134
var addEvent = function(el, name, cb) {
@@ -115,13 +139,6 @@
115139
}
116140
};
117141

118-
var onScroll = function() {
119-
if (element.offsetWidth != lastWidth || element.offsetHeight != lastHeight) {
120-
changed();
121-
}
122-
reset();
123-
};
124-
125142
addEvent(expand, 'scroll', onScroll);
126143
addEvent(shrink, 'scroll', onScroll);
127144
}

0 commit comments

Comments
 (0)