# Math.abs vs bitwise (v23)

## Description

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

Updates from previous revision:

1) None of the previous revisions used negative numbers for their test data, and so cannot be considered as accurate.

2) Switched the for..loop code in each test for a counter to remove the loop-within-a-loop, while still ensuring each test uses the same data.

3) Removed the tests that cached the Math object and Math.abs function, as they were slower than the bitwise version in every result.

4) Added 3 new methods from the article and associated user feedback here.

Note: The 3 new methods, abs2(), abs3(), and abs4(), are NOT suitable for use with floating point numbers. abs2() will truncate all f.p. numbers; abs3() and abs4() will truncate negative f.p. numbers. If this is not desirable, stick with the existing method, which I've renamed to abs1().

## Preparation HTML

``````<script type="text/javascript">
var nums = [];
var numIndex;

var largestDeltaFromZero = 1000; // Values are in the range -1000 to +1000
for( var loop = 0; 1000 > loop; ++loop ) {
var theVal = Math.random() * largestDeltaFromZero * 2 - largestDeltaFromZero;
nums.push( theVal );
}

// Existing method from previous revisions. Works with both integer and floating point numbers
function abs1( x ) {
return ( x < 0 ? -x : x );
}

// New method. Truncates all floating point numbers to integers
function abs2( x ) {
var b = x >> 31;
return ( x ^ b ) - b;
}

// New method. Truncates negative floating point numbers to integers
function abs3( x ) {
return ( 0 > x )? ( ~x + 1 ): x
}

// New method. Truncates negative floating point numbers to integers
function abs4( x ) {
return ( 0>= x )? ( ~x + 1 ): x;
}

// Native JS implementation test.
var abs5 = Math.abs;

// New Method. Works with both integer and floating point numbers
function abs6( n ) {
if ( n >= 0 ) return n;
return 0 - n;
}

// Native JS bound
var abs7 = Math.abs.bind( self );
</script>``````

## Setup

``numIndex = nums.length - 1;``

## Test runner

Testing in
TestOps/sec
abs1
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = abs1(num);``````
abs2
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = abs2(num);``````
abs3
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = abs3(num);``````
abs4
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = abs4(num);``````
cached Math.abs
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = abs5(num);``````
abs6
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = abs6(num);``````
Math.abs
``````if (!numIndex) numIndex = nums.length - 1;
var num = nums[numIndex--];
var absNum = Math.abs(num);``````
``````if (!numIndex) numIndex = nums.length - 1;