Updated August, 2012:
I did some profiling with these implementations:
/* Test 1: */ b = Math.abs(a);
/* Test 2: */ b = abs(a); //local copy: abs = Math.abs;
/* Test 3: */ b = a < 0 ? a * -1 : a;
/* Test 4: */ b = a < 0 ? -a : a;
I got the following result on Windows 7. Values are normalized after the fastest result per browser to make it easier to compare which method is faster:
1:Math 2:abs 3:*-1 4:- 1.0= Version
Chrome 1.0 1.0 1.0 1.0 111ms 21.0.1180.75 m
Firefox 1.0 1.0 1.2 1.2 127ms 14.0.1
IE 1.4 1.0 1.1 1.0 185ms 9.0.8112
Opera 1.9 1.6 1.1 1.0 246ms 12.00
Safari 1.6 1.6 1.1 1.0 308ms 5.1.7
Conclusion:
When I did this test 3 years ago, -a was fastest, but now Math.abs(x) is faster in Firefox! In Chrome abs(a) and -a got the same time and it was only 3 ms difference to the slowest method when I tested it with 10 000 000 numbers.
My Recommendation: Use Math.abs(a). If you are in a tight loop and by profiling has found it to be too slow, you can use a local reference to the abs function:
var abs=Math.abs; //A local reference to the global Math.abs function
for (i=0;i<1234567890;++i) if ( abs( v[i] ) > 10) ++x;
Answer from some on Stack OverflowWhich is faster: Math.abs(value) or value * -1 ?
What is math.abs exactly doing?
What is the real purpose of an absolute value?
The absolute value of the difference between two numbers (like |x-y|) gives the distance between two real numbers on the number line. This is important especially in calculus and real analysis. To give you some terms to look up if you're interested, the absolute value is a metric if we want to treat the real numbers as a metric space (technically incorrect, it is the absolute difference of two numbers which is a metric), and it is an example of a norm if we want to treat the real numbers as a vector space over themselves. Hope that answers your question!
More on reddit.comShould I be using Math.abs?
Videos
Updated August, 2012:
I did some profiling with these implementations:
/* Test 1: */ b = Math.abs(a);
/* Test 2: */ b = abs(a); //local copy: abs = Math.abs;
/* Test 3: */ b = a < 0 ? a * -1 : a;
/* Test 4: */ b = a < 0 ? -a : a;
I got the following result on Windows 7. Values are normalized after the fastest result per browser to make it easier to compare which method is faster:
1:Math 2:abs 3:*-1 4:- 1.0= Version
Chrome 1.0 1.0 1.0 1.0 111ms 21.0.1180.75 m
Firefox 1.0 1.0 1.2 1.2 127ms 14.0.1
IE 1.4 1.0 1.1 1.0 185ms 9.0.8112
Opera 1.9 1.6 1.1 1.0 246ms 12.00
Safari 1.6 1.6 1.1 1.0 308ms 5.1.7
Conclusion:
When I did this test 3 years ago, -a was fastest, but now Math.abs(x) is faster in Firefox! In Chrome abs(a) and -a got the same time and it was only 3 ms difference to the slowest method when I tested it with 10 000 000 numbers.
My Recommendation: Use Math.abs(a). If you are in a tight loop and by profiling has found it to be too slow, you can use a local reference to the abs function:
var abs=Math.abs; //A local reference to the global Math.abs function
for (i=0;i<1234567890;++i) if ( abs( v[i] ) > 10) ++x;
I would suggest picking the method that more clearly shows your intention, rather than worrying about the performance. In this case, the performance gain of multiplying by -1 is probably minimal.
When you use Math.abs(), it is very clear that you want a positive value. When you use * -1 it is not clear, and requires more investigation to determine if the input value is always negative.