Math.abs vs custom abs function

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var int1 = -12345;
    var int2 = 12345;
    var float1 = -12345.67;
    var float2 = 12345.67;
   
    function customAbs1(x) {
      return x < 0 ? x * -1 : x;
    }
   
    function customAbs2(x) {
      return Number(x < 0 ? x * -1 : x);
    }
};
</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.abs
Math.abs(int1);
Math.abs(int2);
Math.abs(float1);
Math.abs(float2);
pending…
customAbs1
customAbs1(int1);
customAbs1(int2);
customAbs1(float1);
customAbs1(float2);
pending…
customAbs2
customAbs2(int1);
customAbs2(int2);
customAbs2(float1);
customAbs2(float2);
pending…
customAbs (no function)
(int1 < 0 ? int1 * -1 : int1);
(int2 < 0 ? int2 * -1 : int2);
(float1 < 0 ? float1 * -1 : float1);
(float2 < 0 ? float2 * -1 : float2);
pending…
customAbs (no function, minus)
(int1 < 0 ? -int1 : int1);
(int2 < 0 ? -int2 : int2);
(float1 < 0 ? -float1 : float1);
(float2 < 0 ? -float2 : float2);
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