Pretty straightforward, but I just want to know which is faster.
I think simply multiplying a number by -1
is much faster than calling a predefined meth
Just the *-1
operation is probably faster, but keep in mind that the end result differs from the result for math.abs().
math.abs(-5)
and math.abs(5)
both return 5.
-5 * -1
returns 5 as well.
5 * -1
returns -5.
So unless you're absolutely sure that the number is negative to begin with, you have to do some tests, which cost more time. Might as well do math.abs().
But really, if the performance difference between abs() and *-1 matters in your JS, your probably have more serious issues.