Which is faster: Math.abs(value) or value * -1 ?

前端 未结 5 1814
天涯浪人
天涯浪人 2020-12-25 11:24

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

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 12:08

    A style question: Why use a * -1 instead of -a? Apart from that I agree that you should use abs() if you don't know the sign of the number beforehand. I would not care for speed but for readability.

提交回复
热议问题