Cache returned value

JavaScript performance comparison

Test case created by Michael Poland

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var matM = function(a,b) {
        var p1,p2,p3,p4,p5,p6,p7,p8,p9 = 0.0;
   
        p1 = a[0]*b[0] + a[1]*b[3] + a[2]*b[6];
        p2 = a[0]*b[1] + a[1]*b[4] + a[2]*b[7];
        p3 = a[0]*b[2] + a[1]*b[5] + a[2]*b[8];
   
        p4 = a[3]*b[0] + a[4]*b[3] + a[5]*b[6];
        p5 = a[3]*b[1] + a[4]*b[4] + a[5]*b[7];
        p6 = a[3]*b[2] + a[4]*b[5] + a[5]*b[8];
   
        p7 = a[6]*b[0] + a[7]*b[3] + a[8]*b[6];
        p8 = a[6]*b[1] + a[7]*b[4] + a[8]*b[7];
        p9 = a[6]*b[2] + a[7]*b[5] + a[8]*b[8];
   
        return [
                p1,p2,p3,
                p4,p5,p6,
                p7,p8,p9
        ];
    }
   
    var matM2 = function(a,b) {
      return [
      a[0]*b[0] + a[1]*b[3] + a[2]*b[6],
        a[0]*b[1] + a[1]*b[4] + a[2]*b[7],
        a[0]*b[2] + a[1]*b[5] + a[2]*b[8],
      a[3]*b[0] + a[4]*b[3] + a[5]*b[6],
        a[3]*b[1] + a[4]*b[4] + a[5]*b[7],
        a[3]*b[2] + a[4]*b[5] + a[5]*b[8],
        a[6]*b[0] + a[7]*b[3] + a[8]*b[6],
        a[6]*b[1] + a[7]*b[4] + a[8]*b[7],
        a[6]*b[2] + a[7]*b[5] + a[8]*b[8]
      ];
    }
};
</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
Inline return
var randX = Math.random();
var randY = Math.random();

function mul(x,y) {
  return x*y*x*y*x*y;
}

mul(randX,randY);
pending…
Cached values return
var randX = Math.random();
var randY = Math.random();

function mul(x,y) {
  var retVal = x*y*x*y*x*y;
  return retVal;
}

mul(randX,randY);
pending…
Matrix multiplication - cache returned value
var matrix1 = [
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random()
];

var matrix2 = [
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random()
];

matM(matrix1, matrix2);
pending…
Matrix multiplication - direct return
var matrix1 = [
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random()
];

var matrix2 = [
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random(),
  Math.random(), Math.random, Math.random()
];

matM2(matrix1, matrix2);
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