re: Use $ & $$ Instead of document.querySelector/All in JavaScript without jQuery VIEW POST


Instead of using 2 different constants, what about checking the length of the selector first like this:

const $ = s => document.querySelectorAll.bind(document)(s).length > 1 ? document.querySelectorAll.bind(document)(s) : document.querySelector.bind(document)(s);

or something like that to make it much more similar to jQuery $?


From a performance perspective, that would double the amount of work for every selector. Going from jQuery to Vanilla JS is mostly about being more performant, so that looks like an anti-pattern.


Can you explain how would that be the double amount of work? It's just one extra call.

It's 2 calls instead of one, so it's double.

Code of Conduct Report abuse