3 ms·
Given how optimized jquery is, does anyone understand why something as simple as getting an element by id would be so much slower than Dom methods?
by aneth4 14y ago
Given how optimized jquery is, does anyone understand why something as simple as getting an element by id would be so much slower than Dom methods?
- dgreensp 14y agoI'm surprised by the number of comments like this. jQuery has to parse the selector and figure out that it's of the form "#id". This requires running a regular expression. A lot more is happening. The whole jQuery call from start to finish takes 2.85 microseconds, in what is presumably a real benchmark, but microbenchmarks like this are hard to interpret and basically meaningless. But yes, if your app needs to do a burst of 350,000 jQuery calls in a tight loop and you are bummed that the whole thing takes a full second, you should then optimize using document.getElementById.
- lukeschlather 14y ago> I'm surprised by the number of comments like this. The sentiment is borne out by the facts when you dig a little deeper. Just calling document.getElementById without using the return value does not actually dig into the DOM and find the element according to this comment: http://news.ycombinator.com/item?id=4436438 http://news.ycombinator.com/item?id=4436438
- aneth4 14y ago> if your app needs to do a burst of 350,000 jQuery calls in a tight loop and you are bummed that the whole thing takes a full second, you should then optimize using document.getElementById Seems like you might have some other optimization work to do at that point :)