Code example:
$JK("body *").each(function() { ... });
Will JQuery fill the elements’ array first before calling each() or there is kinda LINQ-style optimization, so that function will be called during the DOM tree traversal? I guess that this optimization might (or not) be deployed into the JS engine. If so, how one can know if it is implemented for this or that engine/browser?
It builds the entire list first. I can’t imagine how it could be done in the browser/JavaScript engine, though I guess it might be possible to make NodeList be lazy somehow.
The jQuery object looks like an Array instance (it isn’t, but it tries to look like one), meaning that you can randomly-access elements by numeric index. It’d be quite a trick to make that be lazy.
Note that modern browsers support DOM-accessing functions that push a lot of the work down into the browser’s internals, so that often — as, I suspect, in the case of your selector, which is really just
the browser hands back the complete list to jQuery. That’s why I mentioned making the NodeList object be lazy, but I still think it’d be pretty hard given the semantics of JavaScript.