5 ms·
Also never use Math.pow for squaring numbers (used in the distance function) because it is significantly slower as it is designed for abitrary real exponents, n
by tim1994 8y ago
Also never use Math.pow for squaring numbers (used in the distance function) because it is significantly slower as it is designed for abitrary real exponents, not just integer ones. Cannot be learned soon enough IMO :)
Still a very good job for just a week of coding though.
- therein 8y agoDefinitely great job. Reminds me of when I taught my physicist friend C and he wrote a raytracer and a game to demo it the next day. I had never witnessed so many trig functions used on a global context.
- Sean1708 8y agoDoes the JS JIT not handle this? Seems like it would be incredibly trivial...
- fenomas 8y ago> Also never use Math.pow for squaring numbers (used in the distance function) because it is significantly slower... Modern JS engines optimize hot functions at runtime - advice like this won't affect actual performance.
- ghusbands 8y agoThis advice is incorrect. Experimenting in the console in both Firefox and Chrome shows that there's not a significant speed difference between the two, even if you make sure you only call a given function with integers <10000.
- tim1994 8y agoOn my laptop it is about 30% slower (tested with Firefox 64). However I agree, this was not very good advice for someone who just started programming with JS...