4 ms·
Maybe I'm neurotic but if I know I can speed up my application 100x by typing .addEventListener('click', fn) instead of .click(fn) I'm going to do that every ti
by MatthewPhillips 14y ago
Maybe I'm neurotic but if I know I can speed up my application 100x by typing .addEventListener('click', fn) instead of .click(fn) I'm going to do that every time and not lose any sleep over it.
- masklinn 14y ago> Maybe I'm neurotic but if I know I can speed up my application 100x by typing .addEventListener('click', fn) instead of .click(fn) You can't though, not even close. You can speed up your application significantly by using event delegation on a parent instead of binding an event to each element, but the raw DOM makes this a giant pain in the ass in the general case.
- timruffles 14y agoIn case anyone reading doesn't realise* - that's not the kind of thing you'd be doing to get a speed up. I can't think of how addEventListener could lead to a 1.001x speedup of an app, let alone a 100x. You don't ever need to create listeners in loops, so a single operation like that just isn't a optimisation target. If you need to listen for an event that could come from lots of nodes, listen for the event bubbling up to the parent of those nodes. * I'm sure Matthew realises but just gave the first example that came to him.
- MatthewPhillips 14y agoI didn't mean event listeners specifically, I meant trading all of the normal DOM APIs for less verbose and more consistent jQuery alternatives is a trade of performance. On a fast desktop computer that tradeoff is an easy one to make, but in mobile it still arguably isn't.
- tracker1 14y agoFor mobile, try zepto... seriously.. it's not worth the DOM pain even there.
- jimbokun 14y agoDoes that really speed up your application by 100x?
- MatthewPhillips 14y agoSome other posters are claiming 100x performance gain by using DOM API over jQuery, I make no claims to their accuracy.
- timruffles 14y agoNo way - even if it was the sole performance bottleneck in your application (which as I've commented above is absurd, and a sign you're doing it so very, very wrong). Even if your application was: var i = 100; while(i--) els[i].addEventListener("click",fn); it'd be untrue jQuery is nowhere _near_ 100x slower. Profiling this is totally ridiculous because there's no use case that requires adding event listeners at a scale that could _ever_ be a performance concern, but anyway - http://jsperf.com/jquery-on-vs-native/3 http://jsperf.com/jquery-on-vs-native/3. So just 10x slower on even this - completely not performance critical and therefore nobody would bother optimising it - method.