If I know there is at most 1 such object in my DOM, should I always append :first to the selector to speed up the lookup? I’m assuming the selection engine will stop as soon as it finds 1 matching element, but I’m not sure if the filter actually slow things down a bit.
Share
No, as
:firstis not a standard CSS pseudo-class, and using it will cause your selector to not be passed to the nativequerySelectorAll()DOM function implemented by supporting browsers (assuming you don’t use any other jQuery-only selector syntax).jQuery will take the entire selector and parse it by itself (using Sizzle, most likely), which is leagues slower than letting the browser do the work.