forEach shim vs optimized shim

JavaScript performance comparison

Revision 6 of this test case created by foob

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var undefined = void 0;
    var array = [1,2,3,4,5,6,7,8,9,0];
   
    Array.prototype.forEachNormal = function( callback, thisArg ) {
        var T=thisArg, k;
        if ( this == null ) {
          throw new TypeError( "this is null or not defined" );
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if ( {}.toString.call(callback) !== "[object Function]" ) {
          throw new TypeError( callback + " is not a function" );
        }
        k = 0;
        while( k < len ) {
          var kValue;
          if ( k in O ) {
            kValue = O[ k ];
            callback.call( T, kValue, k, O );
          }
          k++;
        }
      };
   
    Array.prototype.forEachAvoidIn = function( callback, thisArg ) {
        var T=thisArg, k;
        if ( this == null ) {
          throw new TypeError( "this is null or not defined" );
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if ( {}.toString.call(callback) !== "[object Function]" ) {
          throw new TypeError( callback + " is not a function" );
        }
        k = 0;
        while( k < len ) {
          var kValue = O[ k ];
          if ( kValue !== undefined || (k in O) ) {
            callback.call( T, kValue, k, O );
          }
          k++;
        }
      };
   
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
forEach
array.forEachNormal(function(val){
    return val;
});
pending…
forEach Avoid 'in'
array.forEachAvoidIn(function(val){
    return val;
});
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. Here’s a list of current revisions for this page:

0 comments

Add a comment