Is Math.pow more expensive than multiplication using temporary assignments?

When porting a javascript library to Python, I found this code:

return Math.atan2( Math.sqrt( (_ = cosφ1 * sinΔλ) * _ + (_ = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * _ ), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ ); 

Am I mistaken or (_ = cosφ1 * sinΔλ) * _ can be written as Math.pow(cosφ1 * sinΔλ, 2) ?

I think the author is trying to avoid using Math.pow, is it expensive in javascript compared to temporary use?

[update]

As of the end of 2016, with Chrome 53.0 (64-bit) it looks like the difference is not as big as before.

+4
source share
1 answer

The only reason I can think of is performance. First give a test if they really do the same, and we didn’t notice anything.

 var test = (test = 5 * 2) * test; // 100 Math.pow(5 * 2, 2); // 100 

As expected, it has proven its worth. Now let's see if they have different characteristics using jsperf. Take a look here: http://jsperf.com/...num -self

The differences for Firefox 23 are very small, but for Safari the difference was much larger. Using Math.pow seems more expensive.

+7
source

Source: https://habr.com/ru/post/1498397/


All Articles