Integrated utility function vs. utility function

JavaScript performance comparison

Test case created by Stefan Judis

Preparation code

<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var ExampleObject = function( array ) {
     this.array = array; 
  };
  
  // the methods we want to implement
  // in our object
  var utilityMethods = [ 'indexOf', 'filter' ],
      slice          = [].slice;
  
  // iterate over the wished methods
  _.each( utilityMethods, function( method ) {
    ExampleObject.prototype[ method ] = function() {
      var args = slice.call( arguments );
      
      args.unshift( this.array );
      
      return _[ method ].apply( _, args );
    };
  });
  
  // create the new object
  var example = new ExampleObject( [ 5, 4, 3, 2, 1 ] );

};
</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
Integrated utility function
for( var i = 0; i < 10000; ++i ) {
  example.indexOf( 2 );
  example.filter( function( val ) {
    return val % 2;
  });
}
pending…
Utility function
for( var i = 0; i < 10000; ++i ) {
  _.indexOf( example.array, 2 )
  _.filter( example.array, function( val ) {
    return val % 2;
  });
}
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