object vs variable as function arguments

JavaScript performance comparison

Test case created by Michael Poland

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    /*
      VECTOR
    */

    var Vector3 = function(_x, _y, _z) {
        return {
          x: _x,
          y: _y,
          z: _z
        };
        };
   
    /*
      EMPTY PROCESSOR FUNCTION
    */

   
    function processObj(obj) {
      var retVal = obj.x + "," + obj.y + "," + obj.z;
      return retVal;
    }
   
    function processAto(x, y, z) {
      var retVal = x + "," + y + "," + z;
      return retVal;
    }
   
   
    /*
      INIT DATA
    */

    var len = 100000;
    var vector3Arr = [];
    for (var i = 0; i < len; i++) {
      var xRand = Math.random();
      var yRand = Math.random();
      var zRand = Math.random();
      var v3 = Vector3(xRand, yRand, zRand);
      vector3Arr.push(v3);
    }
};
</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
Object
var index = Math.floor(Math.random() * (len - 1)) + 0;
var item = vector3Arr[index];
processObj(item);
pending…
Variable
var index = Math.floor(Math.random() * (len - 1)) + 0;
var item = vector3Arr[index];
processAto(item.x, item.y, item.z);
pending…

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

Compare results of other browsers

0 comments

Add a comment