Absolute value speed

JavaScript performance comparison

Revision 8 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 < 0 ? -a : a;
i = b < 0 ? -b : b;
i = c < 0 ? -c : c;
pending…
Bitwise 1
i = (a >>> 31) ? a ^ -1 : a;
i = (b >>> 31) ? b ^ -1 : b;
i = (c >>> 31) ? c ^ -1 : c;
 
pending…
Bitwise 2
i = (a >>> 31) ? -a : a;
i = (b >>> 31) ? -b : b;
i = (c >>> 31) ? -c : c;
pending…
Bitwise 3
i = a < 1 ? a ^ -1 : a;
i = b < 0 ? b ^ -1 : b;
i = c < 0 ? c ^ -1 : 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