Skip To Main Content

jQuery: Fastest Way To Select Nothing

Posted by Rick Waldron

Aug 29 2010

As I become more and more obsessed with Quora, I’m finding more and more questions that I answer seem to feel right as standalone blog posts. The latest even includes an interesting discovery: What’s the cleanest way to intentionally and reliably return a jQuery object that selects nothing?

There were two answers already posted to the question, suggesting the following methods to achieve an empty jQuery set:

  • $([])
  • $(“null”)

While the previous answers will definitely return a jQuery object that selects nothing, neither of them is best choice for performance. The answer is actually on line 79 of the jQuery source, it’s the first condition that jQuery ever evaluates:


// Handle $(""), $(null), or $(undefined)
if ( !selector ) {
        return this;

I thought it might be beneficial to benchmark these against the suggestions posted above; here is the test files and results:

I added my own $(false) technique to the equation as well; I’m pleased with the results:


// Results at 10,000,000 iterations

// $([]) omitted/not tested - caused chrome to crash
// $('null') omitted/not tested - caused chrome to crash

$(""):        1274ms
$(""):        1270ms
$(""):        1278ms
$(""):        1265ms

$(undefined): 1199ms
$(undefined): 1167ms
$(undefined): 1191ms
$(undefined): 1158ms

$(null):      1134ms
$(null):      1221ms
$(null):      1201ms
$(null):      1353ms

$(false):     1055ms
$(false):     1140ms
$(false):     1147ms
$(false):     1137ms

$(): 1203ms
$(): 1309ms
$(): 1161ms
$(): 1318ms

new jQuery.fn.init: 917ms
new jQuery.fn.init: 1050ms
new jQuery.fn.init: 939ms
new jQuery.fn.init: 919ms


// Results at 1,000,000 iterations

$([]): 1552ms
$([]): 1163ms
$([]): 1188ms
$([]): 1146ms

$("null"): 8082ms
$("null"): 4418ms
$("null"): 7643ms
$("null"): 7875ms

$(""): 132ms
$(""): 126ms
$(""): 131ms
$(""): 116ms

$(undefined): 123ms
$(undefined): 118ms
$(undefined): 133ms
$(undefined): 119ms

$(null): 161ms
$(null): 154ms
$(null): 148ms
$(null): 173ms

$(false): 116ms
$(false): 118ms
$(false): 119ms
$(false): 104ms

$(): 127ms
$(): 150ms
$(): 127ms
$(): 172ms

new jQuery.fn.init: 87ms
new jQuery.fn.init: 94ms
new jQuery.fn.init: 98ms
new jQuery.fn.init: 95ms

In conclusion, I can say with complete confidence that $(false) is the fastest and that $(‘null’) is the definitely the worst, as it will attempt to look through entire document, coming up empty handed, but exhausting itself.


Thanks to James Padolsey for mentioning new jQuery.fn.init, which is clearly the winner – I’ve added it’s scores to the test results above.

Thanks to Michael Mahemoff for setting up a JS Perf Case.

Posted by
Rick Waldron
on August 29th, 2010


We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.

Contact Us

We'd love to hear from you. Get in touch!