ajquery.js
ajquery.js copied to clipboard
Use Array.from() in $$
This enables .map()
and .filter()
and suchlike against arrays of matching elements.
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?
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 Has a really good point - it could have a significant speed impact.
Quick, someone do some benchmarks!
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.)
The gods of #webperf have spoken! Make it so!
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');
// []