Absolute value speed

JavaScript performance comparison

Revision 7 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function abs(n){
    return n < 0 ? -n : n;
    }
   
    function bit_abs(n){
    //var mask = n >> 31;
    return (n & 0x7fffffff);
    }
   
};

Benchmark.prototype.teardown = function() {
    var a = 2.73, b = -3.51, c = 0
    var i;
};
</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
Math
i = Math.abs(a);
i = Math.abs(b);
i = Math.abs(c);
pending…
Ternary expression
i = a < a ? -a : a;
i = b < b ? -b : b;
i = c < c ? -c : c;
pending…
Bitwise operations
i = a & 0x7fffffff;
i = b & 0x7fffffff;
i = c & 0x7fffffff;
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:

0 comments

Add a comment