Math.abs-vs-bitwise (v4)

Revision 4 of this benchmark created by Daniel Davis on


Description

Checking performance differences between Math.abs and using Bitwise manipulation.

Preparation HTML

<script>
  var nums = [];
  
  for (var i = 0; i < 200; i++) {
   nums.push(Math.random() * 100);
  }
  
  function abs(x) {
   return (x < 0 ? -x : x);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Math.abs()
var o;

for (var i = 0; i < 200; i++) {
 o = Math.abs(nums[i]);
}
ready
bitwise
var o;

for (var i = 0; i < 200; i++) {
 o = abs(nums[i]);
}
ready
Cached Math
var o;
var m = Math;

for (var i = 0; i < 200; i++) {
 o = m.abs(nums[i]);
}
ready
Cached Math.abs
var o;
var mAbs = Math.abs;

for (var i = 0; i < 200; i++) {
 o = mAbs(nums[i]);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.