~Array.indexOf (bitwise ~)

Benchmark created by Alexis Deveria on


Description

Like http://jsperf.com/tildeindexof but for arrays, also includes an if statement since it the tilde does not return a boolean, so a possible type conversion may affect performance.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var arr = ['a', 'b', 'c', 'd', 'e'];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
~indexOf
if (~arr.indexOf('c')) {};
if (~arr.indexOf('z')) {};
ready
indexOf !== -1
if (arr.indexOf('c') !== -1) {};
if (arr.indexOf('z') !== -1) {};
ready
indexOf > -1
if (arr.indexOf('c') > -1) {};
if (arr.indexOf('z') > -1) {};
ready
indexOf === -1
if (arr.indexOf('c') === -1) {};
if (arr.indexOf('z') === -1) {};
ready
indexOf >= 0
if (arr.indexOf('c') >= 0) {};
if (arr.indexOf('z') >= 0) {};
ready
1+indexOf
if (1 + arr.indexOf('c')) {};
if (1 + arr.indexOf('z')) {};
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Alexis Deveria on