Do not know how your sqrt
is implemented (not a javascript coder) so what is faster I can only speculate but there are few fast methods out there using "magic numbers" for IEEE 754 float/double
formats and also for integers
for example like in Quake3. That works more or less precise with just few ops on defined intervals and are most likely faster then your sqrt but usable only on specific intervals.
Usual sqrt
implementations are done by:
approximation polynomial
usually Taylor series, Chebyshev, etc expansions are used and the number of therms is dependent on target accuracy. Not all math functions can be computed like this.
iterative approximation
there are few methods like Newton, Babylonian, etc which usually converge fast enough so no need to use too much therms. My bet is your sqrt
use Newtonian approximation.
There are also binary search based computations
Binary search requires the same count of iterations then used bits of number result which is usually more then therms used in approximation methods mentioned above. But binary search for sqrt has one huge advantage and that is it can be done without multiplication (which is significant for bignums...)
There are also other search approximations like:
algebraically using log2,exp2
you can compute pow
from log2,exp2
directly and sqrt(x)=pow(x,0.5)
so see
LUT
You can use piecewise interpolation with precomputed look up tables.
hybrid methods
You can combine more methods together like estimate result with low accuracy approximation polynomial and then search around it (just few bits) with binary search ... But this is meaningful only for "big" numbers (in manner of bits)...
some math operations and constants can be computed with PCA
but I see no point to use it in your case...
Also for more info take a look at related QA:
Do not know what are you computing but fastest sqrt
is when you do not compute it at all. Many computations and algorithms can be rewritten so they do not need to use sqrt
at all or at least not that often (like comparing distances^2 etc...).
For examle if you want to do:
x = Random();
y = sqrt(x);
You can rewrite it to:
y= Random();
x = y*y;
but beware the randomness properties are not the same !!!
Math.sqrt
– Prynne