Number Methods (v8)

Revision 8 of this benchmark created by Kevin Greer on


Setup

Number.prototype.plus = function plus(b) { return this + b; }
    function plus(a, b) { return a + b; }
    function genericPlus(a, b) {
      return typeof a === 'string' ? 'not supported' :
        typeof a === 'number' ? a + b : 'not supported';
    }
    
    var pluses = {
      number: function(a, b) { return a + b; },
      string: function(a, b) { return 'not supported' }
    };
    function genericPlus2(a, b) {
      return pluses[typeof a](a, b);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
As Method
var a = 1;
for ( var i = 0 ; i < 1000000 ; i++ ) a = a.plus(1);
 
ready
As Function
var a = 1;
for ( var i = 0 ; i < 1000000 ; i++ ) a = plus(a, 1);
 
ready
Apply Method
var a = 1;
for ( var i = 0 ; i < 1000000 ; i++ ) a = Number.prototype.plus.call(a, 1);
ready
Typeof
var a = 1;
for ( var i = 0 ; i < 1000000 ; i++ ) a = genericPlus(a, 1);
 
ready
Typeof Map
var a = 1;
for ( var i = 0 ; i < 1000000 ; i++ ) a = genericPlus2(a, 1);
 
ready

Revisions

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

  • Revision 8: published by Kevin Greer on