math abs vs conditional

JavaScript performance comparison

Test case created by Timo

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    math_abs = Math.abs;
    var a = Math.random();
    if(Math.random()<0.5) a=-a;
};
</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
Native
a = Math.abs(a);
pending…
Conditional inline
a = a<0?-a:a;
pending…
math_abs
a = math_abs(a);
pending…
Conditional if
if (a<0) a=-a;
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