Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2012 09:40
Show Gist options
  • Save anonymous/4418578 to your computer and use it in GitHub Desktop.
Save anonymous/4418578 to your computer and use it in GitHub Desktop.
performance of index>-1 VS index!=-1 VS index!==-1 VS index>=0 VS ~index
function loop(f)
{
for(var i = 0; i < 10000; i++)
f(Math.floor(Math.random() * 2 - 1) * eval("1"));
}
console.time(">-1");
loop(function(index) index > -1);
console.timeEnd(">-1");
console.time("!=-1");
loop(function(index) index != -1);
console.timeEnd("!=-1");
console.time("!== -1");
loop(function(index) index !== -1);
console.timeEnd("!== -1");
console.time(">=0");
loop(function(index) index >= 0);
console.timeEnd(">=0");
console.time("~");
loop(function(index) ~index);
console.timeEnd("~");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment