Math.abs

JavaScript performance comparison

Test case created by Kevin Geng

Preparation code

<script>
function randInt(min,max) {
  var range = max - min;
  return Math.floor(Math.random() * range) + min;
}
var abs0 = Math.abs;
function abs1(n) {
  return (n < 0) ? -n : n;
}
function abs2(n) {
  return (n ^ (n >> 31)) - (n >> 31);
}
function abs3(n) {
  return (n < 0) ? ~n+1 : n;
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var x = randInt(-1000,1000);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Math.abs
x = Math.abs(x);
pending…
conditional
x = abs1(x);
pending…
local Math.abs
x = abs0(x);
pending…
bitwise ops (ints only)
x = abs2(x);
pending…
conditional #2 (ints only)
x = abs3(x);
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.

0 Comments