DEV Community

Discussion on: Use $ & $$ Instead of document.querySelector/All in JavaScript without jQuery

Collapse
 
hendrasan profile image
Hendra Susanto

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 $?

Collapse
 
capsule profile image
Thibaut Allender

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.

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

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

Thread Thread
 
capsule profile image
Thibaut Allender

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