Math.abs-vs-bitwise (v5)

Revision 5 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);
}

var m = Math;

var mAbs = Math.abs;
</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;

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

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.