Absolute value speed

JavaScript performance comparison

Revision 11 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 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(Math.random());
i = Math.abs(Math.random());
i = Math.abs(Math.random());
pending…
Ternary
i = tern(Math.random());
i = tern(Math.random());
i = tern(Math.random());
pending…
Bitwise 1
i = bit_1(Math.random());
i = bit_1(Math.random());
i = bit_1(Math.random());
pending…
Bitwise 2
i = bit_2(Math.random());
i = bit_2(Math.random());
i = bit_2(Math.random());
pending…
Bitwise 3
i = bit_3(Math.random());
i = bit_3(Math.random());
i = bit_3(Math.random());
pending…
local ABS
i = localABS(Math.random());
i = localABS(Math.random());
i = localABS(Math.random());
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