should I replace document.getElementById
or so by document.querySelector
?
any difference?
Will you recommend me to use querySelector
?
When you are selecting on ids anyway, use getElementById
as that’s a lot more efficient than using querySelector
on an id selector. The latter runs the whole CSS selector parsing, while the former can just take the ID and get the element with that ID directly.
Of course, when selecting based on other criteria than the element’s id, querySelector
(and querySelectorAll
) obviously has its place.
(The obligatory benchmark to prove this claim, although I do want to note that benchmarks are not everything, and the difference probably won’t make much difference in an actual application.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With