Which is the best way (performance-wise) to get the root document node (the <html>
element) in jQuery? I can think of several methods that may or may not work:
$("html")
$(document.documentElement)
$(document)
(?)
$.root
(?)
$.document
(?)
Which is the best way (performance-wise) to get the root document node (the <html>
element) in jQuery? I can think of several methods that may or may not work:
$("html")
$(document.documentElement)
$(document)
(?)
$.root
(?)
$.document
(?)
$(document.documentElement)
is the fastest, by quite some margin (see tests here).
You can get more insight as to why this is the case by looking at the jQuery source code (look at the init
function, in particular, the part that handles a DOM element, and the part that handles a string).
I don't think these are really that different, but $("html")
seems the most readable, and therefore logical option.
According to Addy Osmani, id
and element
selectors are the quickest.
http://addyosmani.com/jqprovenperformance/
See slides 21 & 25.
So I say $("html")
Agree with @AlienWebguy that you can run your own tests on jsperf.com.