Absolute value speed

JavaScript performance comparison

Revision 10 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var localABS = Math.abs;
   
    function tern(n){
    return n < 0 ? -n : n;
    }
   
    function bit_1(n){
    return (n >>> 31) ? n ^ -1 : n;
    }
   
    function bit_2(n){
    return (n >>> 31) ? -n : n;
    }
   
    function bit_3(n){
    return (n >>> 31) ? n ^ -1 : n;
    }
   
};

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
i = tern(a);
i = tern(b);
i = tern(c);
pending…
Bitwise 1
i = bit_1(a);
i = bit_1(b);
i = bit_1(c);
pending…
Bitwise 2
i = bit_2(a);
i = bit_2(b);
i = bit_2(c);
pending…
Bitwise 3
i = bit_3(a);
i = bit_3(b);
i = bit_3(c);
pending…
local ABS
i = localABS(a);
i = localABS(b);
i = localABS(c);
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