Math.abs-vs-bitwise (v18)

Revision 18 of this benchmark created on


Description

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

Setup

var nums = [];
      
      for (var i = 0; i < 200; i++) {
        var rand = Math.random()
        var isNegative = Math.random() < 0.5
        nums.push(isNegative ? rand : -rand)
      }

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 = nums[i] < 0 ? -nums[i] : nums[i];
}
ready

Revisions

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