getElementById vs querySelector

Personally I find that .getElementById() is a bit more of a statement when reading the code… like, I’m not interested in the first element that happens to match the selector, but in this unique element.

As for performance, getting by ID is definitively faster… I mean technically you don’t even have to use document.getElementById('my-id'), but could also just use window['my-id']… not sure if this is some sort of proxy-behaviour though, or if all references are actually there from the start.

Edit: I just did some performance tests too and it seems the window version is actually by far the slowest… so yet another reason not to use it. ^^

1 Like