I understand that $("#id")
is faster because it maps to a native javascript method. Is the same true of $("body")
?
Asked
Active
Viewed 450 times
4
2 Answers
10
No it does not use Sizzle, there's a special shortcut for $("body")
in place, you can see the code here:
// The body element only exists once, optimize finding it
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
this.selector = "body";
this.length = 1;
return this;
}
Note that this isn't quite the same as $(document.body)
, as the resulting context of $("body")
is document
, where as $(document.body)
(like any other DOM node) has a context of itself.

Nick Craver
- 623,446
- 136
- 1,297
- 1,155
-
Does it use sizzle for $("body.class")? Should I consequently use $("body").filter(".class")... or something? – Matrym Dec 09 '10 at 21:59
-
@Matrym - Newer browsers won't care much either way, they'll use native selector methods, like `querySelectorAll()`. But to answer directly, no `$("body.class")` or any variant *except* `$("body")` isn't specially handled, so it'll get dumped to the selector engine. – Nick Craver Dec 09 '10 at 22:09
6
This is straight from the source (code):
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
this.selector = "body";
this.length = 1;
return this;
}
For tags other than body
If you dig a little deeper it turns out they will use getElementsByTagName
if no context is given. This will give a nice boost to performance over using the Sizzle engine.
// HANDLE: $("TAG")
} else if ( !context && !rnonword.test( selector ) ) {
this.selector = selector;
this.context = document;
selector = document.getElementsByTagName( selector );
return jQuery.merge( this, selector );
// HANDLE: $(expr, $(...))
}

ChaosPandion
- 77,506
- 18
- 119
- 157
-
-
@ChaosPandion - Your updated answer is incorrect, the first version runs if no context is given, providing the `` is present (it would be on `document.ready`). – Nick Craver Dec 09 '10 at 20:15
-
@Nick - Can you explain what you think is incorrect with more detail? – ChaosPandion Dec 09 '10 at 20:19
-
@ChaosPandion - It's just incorrect, `if ( selector === "body" && !context && document.body ) {` is `true` if no context is given, *that's* the code used, not the element selector branch. – Nick Craver Dec 09 '10 at 20:20
-
-
@ChaosPandion - well not so much clarity as a changed meaning :) but yes that's a correct statement now :) Though I do disagree with the performance bit, Sizzle would do the same thing if it was given the selector, with one additional call...so the performance isn't that different. – Nick Craver Dec 09 '10 at 20:25
-
-
-
@Nick - Hahaha I saw that! @Stephan - I don't anymore but jQuery does. – ChaosPandion Dec 09 '10 at 20:38
-
@ChaosPandion - Saw...? Clue me in? For the other question: IE6 has no impact on my tune...you're forgetting one simple fact: the `$("tag")` wasn't optimized until jQuery 1.4, [you can see the commit here](https://github.com/jquery/jquery/commit/4ea4fad0902839c06c281b5de7b0aca29922b63d). Users on jQuery 1.3.2 don't even have it, and they perform just fine on IE6...there's not a huge gain there, it is *some* gain yes, but not a big one. – Nick Craver Dec 09 '10 at 20:43
-
@Nick - I saw that someone said "Make me" in my inbox and they deleted it. I guess it was someone else. Also it isn't a big gain but it is something. – ChaosPandion Dec 09 '10 at 20:52
-
@ChaosPandion - I was typing "Make me understand why you think..." but got an outlook "IMAP Server closed your connection..." freaking annoying error in the middle, me banging enter to make it go away probly resulted in a comment post :) – Nick Craver Dec 09 '10 at 20:54