3 ms·
Browsers do exactly the optimization you're describing: https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/css/selector_qu
by esprehn 5y ago
Browsers do exactly the optimization you're describing:
https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/css/selector_query.cc;l=382;drc=fde2db9853cab5681586ea71d877f4cefd5eee13 https://source.chromium.org/chromium/chromium/src/+/main:thi...
Both getElementById and querySelector are quite fast, down to the level of measuring individual branches in a micro benchmark. querySelector does have to do a bit more work to lookup the cached query and a handful of extra branches over getElementById, it's not scanning the document for an ID query unless you're in quirks mode though.