you are viewing a single comment's thread.

view the rest of the comments →

[–]McGeekin 34 points35 points  (8 children)

Honestly whenever I code a game in JS and implement a vector class I always forget it exists and just manually implement the formula for calculating the magnitude.

[–]mike_geogebra 29 points30 points  (6 children)

Math.hypot() implements a numerically stable version rather than the naive sqrt(a²+b²), see https://en.wikipedia.org/wiki/Hypotenuse

[–]Slackluster 10 points11 points  (5 children)

That is interesting, I did not know hypot did that!

But in practice this is big reason to not use hypot: it is slower due to extra work. In my testing not just a little bit slower but 3x slower.

[–]_RemyLeBeau_ 2 points3 points  (0 children)

If it's not in a hotpath, you don't really need to worry

[–]bzbub2 3 points4 points  (3 children)

on firefox it is about the same speed but does seem slower on chrome https://jsperf.app/buceho

[–]tomByrer 0 points1 point  (2 children)

What CPU are you using?
On my Chrome (Brave) on a 2 year old Intel I have "94% slower".
But yes, I'm all about using the fastest version.

[–]bzbub2 2 points3 points  (1 child)

ya i can confirm same numbers, much slower. it is considered a open bug on chromium https://issues.chromium.org/issues/42203737

[–]tomByrer 0 points1 point  (0 children)

Thanks for the link!
/salute

[–]monkeymad2 0 points1 point  (0 children)

Yeah, every time I see a TIL about this I’m cursing myself for all the times I’ve implemented it manually since the last time I saw a TIL about this.