I have some heavy function attached to scroll event in my application. To limit frequency of it execution I'm using simple debounce method like this:
var timeout;
document.addEventListener("scroll", function () {
clearTimeout(timeout);
timeout = setTimeout(function () {
// some heavy code
}, 200);
});
Is there a better way to do this? Isn't setTimeout too heavy itself to call each time the event fires?
EDIT: what concerns me most is memory usage - i think each setTimeout is allocating some. This is memory allocation from chrome timeline (from this fiddle: http://jsfiddle.net/hky0oekm/):