3 ms·
"Useless" is subjective, but the jQuery object does a lot of extra work above the vanilla example. $ isn't just a syntax layer on top of querySelectorAll. When
by xionon 14y ago
"Useless" is subjective, but the jQuery object does a lot of extra work above the vanilla example.
$ isn't just a syntax layer on top of querySelectorAll. When you do $('p'), it first queries the DOM for everything that matches that selector, then it creates new jQuery objects to wrap each of the returned nodes as well as creating a jQuery object to contain them.
If all you're doing is setting the innerHTML of these objects, it's a non-trivial overhead.
- Xion 14y agoIt creates just one jQuery object to wrap the entire array of DOM nodes. Afterwards, .html() is essentially innerHTML [1], provided jQuery can use it directly (otherwise it has to create DOM nodes by itself). [1] https://github.com/jquery/jquery/blob/master/src/manipulation.js#L219 https://github.com/jquery/jquery/blob/master/src/manipulatio...