ajquery.js icon indicating copy to clipboard operation
ajquery.js copied to clipboard

Use Array.from() in $$

Open simonw opened this issue 3 years ago • 3 comments

This enables .map() and .filter() and suchlike against arrays of matching elements.

simonw avatar Dec 15 '21 01:12 simonw

Hmm... generally seems like a good idea (who actually wants a C++ NodeList when they were expecting a JavaScript Array after all?).

However, there's one potential bug that this change could cause:

let nodeList = document.querySelectorAll('body');
nodeList.item(0);

Does anyone use nodeList.item(0) rather than nodeList[0]?

I think such people... should probably be shot - or at least put away. But is that just due to my ignorance? Or... amiright?

coolaj86 avatar Dec 15 '21 20:12 coolaj86

This is a good idea, but it would put the main goal of this project at risk: being the fastest, most lightweight jQuery alternative. Is it worth the convenience of being able to map and filter???1!1

jaap3 avatar Dec 16 '21 12:12 jaap3

@jaap3 Has a really good point - it could have a significant speed impact.

Quick, someone do some benchmarks!

coolaj86 avatar Dec 21 '21 00:12 coolaj86

Yes, there is a minuscule overhead (I assume you don't have NodeLists of millions of items), but I think it's worth it. That's because jQuery matched sets do have array-like iteration methods, such as map and filter. (Despite not being actually arrays.)

shappir avatar Jan 02 '23 15:01 shappir

The gods of #webperf have spoken! Make it so!

coolaj86 avatar Jan 02 '23 17:01 coolaj86

I was worried there might be an error when the selector returns a null result but, thankfully, that doesn't actually happen:

document.querySelectorAll('bananas');
// []

coolaj86 avatar Feb 13 '23 18:02 coolaj86