bidirectionalIndexOf

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code

<script>
var array=[],l=100000;
while(l--)array[l]=l;

function bidirectionalIndexOf(a,b,c,d,e){
 for(c=a.length,d=c*1;c--;){
  if(a[c]==b)return c;
  if(a[e=d-1-c]==b)return e;
 }
 return -1
}

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return i;
       }
    }
    return -1;
}

function forContains(a, el) {
    for (var i=0;i<a.length;i++)
    {
        if (a[i] === el) { return i; }
    }
    return -1;
}

function forEachContains(a, el) {
    a.forEach(function(av, ind) {
       if (av === el) { return ind; }
    });
    return -1;
}

function cc5(a,b,c,d){
 c=a.length;d=c-1;
 while(c--){
  if(b===a[c])return c;
  if(b===a[d-c])return d-c;
  if(b===a[c%2?(d+1-c)/2+((d+1)/2)|0:(d+1)/2-(d+1-c)/2|0])return c%2?(d+1-c)/2+((d+1)/2)|0:(d+1)/2-(d+1-c)/2|0;
 }
 return c
}
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
bidirectionalIndexOf
var x = bidirectionalIndexOf(array, 99999);
var y = bidirectionalIndexOf(array, 55555);
var z = bidirectionalIndexOf(array, 1);
var a = bidirectionalIndexOf(array, 77777);
var b = bidirectionalIndexOf(array, 22222);
pending…
indexOf
var x = array.indexOf(99999);
var y = array.indexOf(55555);
var z = array.indexOf(1);
var a = array.indexOf(77777);
var b = array.indexOf(22222);
pending…
lastIndexOf
var x = array.lastIndexOf(99999);
var y = array.lastIndexOf(55555);
var z = array.lastIndexOf(1);
var a = array.lastIndexOf(77777);
var b = array.lastIndexOf(22222);
pending…
contains
var x = contains(array, 99999);
var y = contains(array, 55555);
var z = contains(array, 1);
var a = contains(array, 77777);
var b = contains(array, 22222);
pending…
test 3 checks per iteration
var x = cc5(array, 99999);
var y = cc5(array, 55555);
var z = cc5(array, 1);
var a = cc5(array, 77777);
var b = cc5(array, 22222);
pending…
forContains
var x = forContains(array, 99999);
var y = forContains(array, 55555);
var z = forContains(array, 1);
var a = forContains(array, 77777);
var b = forContains(array, 22222);
pending…
forEachContains
var x = forEachContains(array, 99999);
var y = forEachContains(array, 55555);
var z = forEachContains(array, 1);
var a = forEachContains(array, 77777);
var b = forEachContains(array, 22222);
pending…

Compare results of other browsers

Revisions

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

0 Comments