Are there any alternatives to using Math.sqrt()
to get the square root of an unknown value?
For example:
var random = (Math.random() * (999 - 1)) + 1;
var sqrt = Math.sqrt(random);
I've heard that using Math.sqrt()
to get the square root of a number is a very slow operation, I'm just wondering if there are any faster ways I can get the square root of a random number. Any help with this would be greatly appreciated.
I don't think you can get any faster than the built in pre-compiled code yet for your information below you can find the algorithm on how you might get the square root of a number with pure JS.
It's pretty fast but since it's recursive it will most probably do somewhat slower than it's iterative version. Iterative implementation is up to you.
It utilizes the Babylonian method.