indexOf polyfill check optimization

Benchmark created by Ranjith KR on


Setup

var f = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yza'];
    
    // From: https://github.com/eligrey/classList.js/blob/master/classList.js
    
    function indexOf_eg(item) {
      var
      i = 0,
        len = this.length;
      for (; i < len; i++) {
        if (i in this && this[i] === item) {
          return i;
        }
      }
      return -1;
    };
    
    // from: http://webreflection.blogspot.fr/2011/06/partial-polyfills.html
    
    function indexOf_ag(value) {
      for (var i = this.length; i-- && this[i] !== value;) {}
      return i;
    };
    
    var indexOf_ch = Array.prototype.indexOf;

Test runner

Ready to run.

Testing in
TestOps/sec
method 1 (Eli Grey)
indexOf_eg.call(f, 'yza');
ready
method 2 (Andrea Giammarchi)
indexOf_ag.call(f, 'yza');
ready
Chrome's indexOf in var
indexOf_ch.call(f, 'yza');
ready
Chrome's indexOf
f.indexOf('yza');
ready

Revisions

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

  • Revision 1: published by Ranjith KR on