Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created August 28, 2010 20:13
Show Gist options
  • Save rwaldron/555527 to your computer and use it in GitHub Desktop.
Save rwaldron/555527 to your computer and use it in GitHub Desktop.
// 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
<!DOCTYPE html>
<html>
<head>
<title>jQuery Selects Nothing</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function () {
var iter = 1000000; // change to 10000000 for tsting
console.time('$([])');
for ( var i = 0; i < iter; i++ ) {
var a = $([]);
}
console.log(a instanceof jQuery); //true
console.timeEnd('$([])');
console.time('$("null")');
for ( var i = 0 ; i < iter; i++ ) {
var ns = $('null');
}
console.log(ns instanceof jQuery); // true
console.timeEnd('$("null")');
console.time('$("")');
for ( var i = 0; i < iter; i++ ) {
var es = $("");
}
console.log(es instanceof jQuery); //true
console.timeEnd('$("")');
console.time('$(undefined)');
for ( var i = 0; i < iter; i++ ) {
var u = $(undefined);
}
console.log(u instanceof jQuery); //true
console.timeEnd('$(undefined)');
console.time('$(null)');
for ( var i = 0; i < iter; i++ ) {
var n = $(null);
}
console.log(n instanceof jQuery); //true
console.timeEnd('$(null)');
console.time('$(false)');
for ( var i = 0; i < iter; i++ ) {
var f = $(false);
}
console.log(f instanceof jQuery); //true
console.timeEnd('$(false)');
console.time('$()');
for ( var i = 0; i < iter; i++ ) {
var none = $();
}
console.log(none instanceof jQuery); //true
console.timeEnd('$()');
console.time('new jQuery.fn.init');
for ( var i = 0; i < iter; i++ ) {
var init = new jQuery.fn.init;
}
console.log(init instanceof jQuery); //true
console.timeEnd('new jQuery.fn.init');
});
</script>
</head>
<body>
</body>
</html>
// Handle $(""), $(null), or $(undefined)
if ( !selector ) {
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment