math.abs

Benchmark created on


Setup

var arr = new Array(10000).fill(0).map(e => Math.floor(Math.random()*10000) - 5000);

var outArr = new Array(10000);

Test runner

Ready to run.

Testing in
TestOps/sec
Math.abs
for (let i = 0; i < 10000; i++) {
	outArr[i] = Math.abs(arr[i]);
}
ready
x < 0 ? -x : x
for (let i = 0; i < 10000; i++) {
	var x = arr[i];
	outArr[i] = x < 0 ? -x : x;
}
ready
bitwise
for (let i = 0; i < 10000; i++) {
	var x = arr[i];
	outArr[i] = (x ^ (x >> 31)) - (x >> 31);
}
ready

Revisions

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