Skip to content

Instantly share code, notes, and snippets.

@Rob-ot
Created June 15, 2011 18:53
Show Gist options
  • Save Rob-ot/1027804 to your computer and use it in GitHub Desktop.
Save Rob-ot/1027804 to your computer and use it in GitHub Desktop.
javascript tilde operator with indexOf
> var a = ["a", "b"]
> a.indexOf("a")
0
> a.indexOf("b")
1
> a.indexOf("c")
-1
> ~a.indexOf("a")
-1
> ~a.indexOf("c")
0
> !!~a.indexOf("a")
true
> !!~a.indexOf("c")
false
@vikstrous
Copy link

console.time('a');
for(var n=0; n<100000; n++){
    !!~ -1;
}
console.timeEnd('a');
console.time('b');
for(var n=0; n<100000; n++){
    0 !== -1;
}
console.timeEnd('b');

Firefox:
a: 232ms
b: 95ms

Chrome:
a: 265ms
b: 152ms

Just saying...

@hassanbazzi
Copy link

^^

Since then,

Most browsers perform these much faster and equally as fast too.

@ISNIT0
Copy link

ISNIT0 commented Sep 23, 2016

I guess this shows just how much browsers (and computers in general) have improved in just a few years!

image
Chrome on Windows 7

@Fenchurchh
Copy link

image
NodeJS on Win10. My chrome benchmark equals the bencmark from ISNIT0.

Just wondering why nodejs is ~2-3x faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment