Math.pow(float, 4) vs. multiplying float * float * float * float

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var test_cases = [
        0,
        1,
        3/2,
        2/3,
        5/7,
        -9/6,
        Math.PI,
        Math.PI * 256,
        Math.PI * 65536,
        Math.PI * Math.E * 4294967296,
        Math.E, Math.SQRT2,
        Math.SQRT2 * 4294967296,
        Math.SQRT1_2,
        Math.LN2,
        Math.LN10,
        Math.LOG2E,
        Math.LOG10E,
        (1 + Math.sqrt(5)) / 2
    ];
};
</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
power: Math.pow(f, 4)
for (var i in test_cases) {
  Math.pow(test_cases[i], 4);
}
pending…
multiply: f * f * f * f
for (var i in test_cases) {
  test_cases[i] * test_cases[i] * test_cases[i] * test_cases[i];
}
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:

1 comment

tomByrer commented :

Sorry to say, but this test is not accurate since it really tests if looking up an array 1 time is faster than looking up an array 4 times. See version 2.

Add a comment