bidirectional (v9)

Revision 9 of this benchmark created by Patrick Denny on


Preparation HTML

<script>
  var elem,
      i = 0,
      allElements = document.getElementsByTagName("*"),
      haystack = [];

  // Sizzle Expr.find["TAG"]
  while ( (elem = allElements[i++]) ) {
    if ( elem.nodeType === 1 ) {
      haystack.push( elem );
    }
  }
</script>

<script>
  var result,
      lowIndex = haystack[2],
      highIndex = haystack[ haystack.length - 2 ],
      midIndex = haystack[haystack.length / 2 ],
      notFound = document.body.appendChild( document.createElement("noscript") ),
      biIndexOf = function( elems, elem ) {
        var i = -1,
        l = elems.length;
       while (++i < l--) {
          if ( elems[i] === elem ) {
            return i;
          }
          else if (i < l && elems[l] === elem) {
            return l;
          }
        }
        return -1;
      },
      simpleIndexOf = function( elems, elem ) {
        var i = 0,
        len = elems.length;
        for ( ; i < len; i++ ) {
          if ( elems[i] === elem ) {
            return i;
          }
        }
        return -1;
      };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Low index (mine)
result = biIndexOf(haystack, lowIndex);
ready
Low index (thor)
result = simpleIndexOf(haystack, lowIndex);
ready
High index (mine)
result = biIndexOf(haystack, highIndex);
ready
High index (thor)
result = simpleIndexOf(haystack, highIndex);
ready
Middle index (mine)
result = biIndexOf(haystack, midIndex);
ready
Middle Index (thor)
result = simpleIndexOf(haystack, midIndex);
ready
Not Found (mine)
result = biIndexOf(haystack, notFound);
ready
Not Found (thor)
result = simpleIndexOf(haystack, notFound);
ready

Revisions

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