Without further details, the question can only be answered in a general sense, I'm afraid.

Do Not Re-Invent the Wheel

So should You, generally speaking, try to re-implement Math.sqrt using lookup tables and whatnot? I would strongly advise against that. You would be hard pressed to even come close to the default JSVM implementation. It's not even a fair fight: The JavaScript Virtual Machine (JSVM) has access to native code and even hardware instructions that You likely do not.

So what can You do? In the following, You will find some suggestions, in the order in which I would personally try them:

Swapping Loop Variables

In Your given example, the order of loop variables is sub-optimal. The better order would be:

for(let i = 0; i < width ; ++i)
for(let j = 0; j < height; ++j)
  array[i][j] = Math.sqrt(/* some expression involving i and j */);

This should be significantly faster because array is actually an Array of Array references, i.e. every time You call array[i] for a different i, a new Array object has to be looked up in memory. It is much more cache friendly to process the inner Arrays one after another.

Motivate the JSVM to use float32

I have not actually tested this, but You might be able to tell the JSVM to use float32 precision instead of float64. One way to do that might be to use a lot of Math.fround calls, something along the lines of:

for(let i = 0; i < width ; ++i)
for(let j = 0; j < height; ++j)
  array[i][j] = Math.fround( Math.sqrt( Math.fround(/* some expression involving i and j */) ) );

You should always benchmark to see if this gives You any performance gains at all.

Another way to to enforce float32 is to use Float32Array as inner arrays. Not only do Float32Arrays give type information to the JSVM. They also have half the memory size compared to Arrays of Numbers, i.e. it should increase Your throughput.

const array = Array.from({length: width}, () => new Float32Array(height));

for(let i = 0; i < width ; ++i)
for(let j = 0; j < height; ++j)
  array[i][j] = Math.sqrt(/* some expression involving i and j */);

For more information on this topic, see this blog post.

Fast inverse square root

If You are using the square root to normalize vectors for computer graphics, there is a bonkers approximation of the inverse square root that was originally used in the Quake 3 engine. It might be faster than 1/Math.sqrt(y). As always, benchmarking is Your best friend.

Math.hypot()

If You do something along the lines of Math.sqrt(x*x + y*y + ...), You can try to use Math.hypot(x,y,...) instead. It might be a tiny bit faster and, on top of that, it is underflow- and overflow-safe.

Use the WebGL/WebGPU/tensorflow.js

If all else fails You can try to use GPU acceleration to speed up Your computations. The easiest way to to that might be using some machine learning framework like tensorflow.js.

import * as tf from '@tensorflow/tfjs';

tf.setBackend('webgl');

const sqrt = await tf.sqrt(array).array();

console.log({sqrt});

Moving data to and from the GPU comes with some overhead, so this might only give performance benefits if You off-load more than just the sqrt operation to the GPU.

You can of course use WebGL or WebGPU direcly, but it will be a lot more work.

Answer from Dirk on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › sqrt
Math.sqrt() - JavaScript | MDN
July 10, 2025 - \forall x \geq 0,\;\mathtt{\operatorname{Math.sqrt}(x)} = \sqrt{x} = \text{the unique } y \geq 0 \text{ such that } y^2 = x
🌐
W3Schools
w3schools.com › jsref › jsref_sqrt.asp
JavaScript Math sqrt() Method
The Math.sqrt() method returns the square root of a number.
Discussions

optimization - JavaScript Math.sqrt performance - Stack Overflow
Through code profiling, I have found the Math.sqrt function specifically to be a major bottleneck in a large doubly-nested loop that runs every timestep in my program. Is there any way to improve its More on stackoverflow.com
🌐 stackoverflow.com
How to implement sqrt() in javascript.
Here's my implementation var math = { sqrt: function(x) { return Math.sqrt(x); } } Pretty sweet if I do say so myself. More on reddit.com
🌐 r/javascript
25
20
January 26, 2011
javascript - Faster Alternative to Math.sqrt() - Stack Overflow
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. ... usually Taylor series, Chebyshev, etc expansions are used and the number of therms is dependent on target accuracy. Not all math functions ... More on stackoverflow.com
🌐 stackoverflow.com
JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt - Stack Overflow
I'm trying to learn algorithms and coding stuff by scratch. I wrote a function that will find square roots of square numbers only, but I need to know how to improve its performance and possibly ret... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Math.js
mathjs.org › docs › reference › functions › sqrt.html
math.js | an extensive math library for JavaScript and Node.js
For matrices, if you want the matrix square root of a square matrix, use the sqrtm function. If you wish to apply sqrt elementwise to a matrix M, use math.map(M, math.sqrt).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-sqrt-method
JavaScript Math sqrt() Method - GeeksforGeeks
July 15, 2024 - The JavaScript Math sqrt( ) Method in JavaScript is used to square the root of the number passed as a parameter to the function.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › sqrt
JavaScript Math sqrt() - Calculate Square Root | Vultr Docs
September 27, 2024 - The Math.sqrt() function in JavaScript is a standard method used to calculate the square root of a number.
🌐
CoreUI
coreui.io › answers › how-to-get-the-square-root-of-a-number-in-javascript
How to get the square root of a number in JavaScript · CoreUI
September 28, 2025 - The Math.sqrt() function returns the square root of a number, which is the value that when multiplied by itself equals the original number. In these examples, Math.sqrt(16) returns 4 because 4 × 4 = 16, and Math.sqrt(2.25) returns 1.5 because ...
Find elsewhere
🌐
Programiz
programiz.com › javascript › library › math › sqrt
JavaScript Math.sqrt() (with Examples)
Become a certified JavaScript programmer. Try Programiz PRO! ... The sqrt() method computes the square root of a specified number and returns it. // square root of 4 let number = Math.sqrt(4); console.log(number); // Output: 2
Top answer
1 of 2
3

Without further details, the question can only be answered in a general sense, I'm afraid.

Do Not Re-Invent the Wheel

So should You, generally speaking, try to re-implement Math.sqrt using lookup tables and whatnot? I would strongly advise against that. You would be hard pressed to even come close to the default JSVM implementation. It's not even a fair fight: The JavaScript Virtual Machine (JSVM) has access to native code and even hardware instructions that You likely do not.

So what can You do? In the following, You will find some suggestions, in the order in which I would personally try them:

Swapping Loop Variables

In Your given example, the order of loop variables is sub-optimal. The better order would be:

for(let i = 0; i < width ; ++i)
for(let j = 0; j < height; ++j)
  array[i][j] = Math.sqrt(/* some expression involving i and j */);

This should be significantly faster because array is actually an Array of Array references, i.e. every time You call array[i] for a different i, a new Array object has to be looked up in memory. It is much more cache friendly to process the inner Arrays one after another.

Motivate the JSVM to use float32

I have not actually tested this, but You might be able to tell the JSVM to use float32 precision instead of float64. One way to do that might be to use a lot of Math.fround calls, something along the lines of:

for(let i = 0; i < width ; ++i)
for(let j = 0; j < height; ++j)
  array[i][j] = Math.fround( Math.sqrt( Math.fround(/* some expression involving i and j */) ) );

You should always benchmark to see if this gives You any performance gains at all.

Another way to to enforce float32 is to use Float32Array as inner arrays. Not only do Float32Arrays give type information to the JSVM. They also have half the memory size compared to Arrays of Numbers, i.e. it should increase Your throughput.

const array = Array.from({length: width}, () => new Float32Array(height));

for(let i = 0; i < width ; ++i)
for(let j = 0; j < height; ++j)
  array[i][j] = Math.sqrt(/* some expression involving i and j */);

For more information on this topic, see this blog post.

Fast inverse square root

If You are using the square root to normalize vectors for computer graphics, there is a bonkers approximation of the inverse square root that was originally used in the Quake 3 engine. It might be faster than 1/Math.sqrt(y). As always, benchmarking is Your best friend.

Math.hypot()

If You do something along the lines of Math.sqrt(x*x + y*y + ...), You can try to use Math.hypot(x,y,...) instead. It might be a tiny bit faster and, on top of that, it is underflow- and overflow-safe.

Use the WebGL/WebGPU/tensorflow.js

If all else fails You can try to use GPU acceleration to speed up Your computations. The easiest way to to that might be using some machine learning framework like tensorflow.js.

import * as tf from '@tensorflow/tfjs';

tf.setBackend('webgl');

const sqrt = await tf.sqrt(array).array();

console.log({sqrt});

Moving data to and from the GPU comes with some overhead, so this might only give performance benefits if You off-load more than just the sqrt operation to the GPU.

You can of course use WebGL or WebGPU direcly, but it will be a lot more work.

2 of 2
0

As your iterating in a 2-dimensional array you can reduce the iterations by ~2.

For instance :

if your expression is i x j, and your array is 3-3 size starting from 0 your going to compute in your loop :

  • 0*1 AND 1*0
  • 0*2 AND 2*0
  • 1*2 AND 2*1

which is the same

0x0 1x0 2x0

0x1 1x1 2x1

0x2 1x2 2x2

🌐
TutorialsPoint
tutorialspoint.com › javascript › math_sqrt.htm
JavaScript Math.sqrt() Method
The Math.sqrt() method in JavaScript accepts a numeric value as its argument and returns the square root of that number (non-negative number). Mathematically, if x is the number passed to Math.sqrt(x), the result is the non-negative number y ...
🌐
Reddit
reddit.com › r/javascript › how to implement sqrt() in javascript.
r/javascript on Reddit: How to implement sqrt() in javascript.
January 26, 2011 - It's a trick question, Math.sqrt() is part of the javascript engine, which means it's compiled from a lower level language and you wouldn't want to implement it in JS, it would be slower by a factor of at least 5.
Top answer
1 of 7
11

You can be sure that the fastest algorithm you will write your self is already implemented within Math.sqrt if not better .

There is an algorithm to go through the numbers till the middle (with some simply calculation) : Writing your own square root function

but as I said, it's probably implemented if not better.

You can try to look for some specific business/domain logic in order to reduce numbers range .

2 of 7
10

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:

  1. 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.

  2. 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

    • Power by squaring for negative exponents

    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...)

    • How to get a square root for 32 bit input in one clock cycle only?

    There are also other search approximations like:

    • How approximation search works
  3. algebraically using log2,exp2

    you can compute pow from log2,exp2 directly and sqrt(x)=pow(x,0.5) so see

    • How Math.Pow (and so on) actually works
  4. LUT

    You can use piecewise interpolation with precomputed look up tables.

  5. 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)...

  6. 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:

  • How is the square root function implemented?

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 !!!

🌐
p5.js
p5js.org › reference › p5 › sqrt
sqrt
A number's square root can be multiplied by itself to produce the original number. For example, sqrt(9) returns 3 because 3 × 3 = 9. sqrt() always returns a positive value. sqrt() doesn't work with negative arguments such as sqrt(-9). ... Number: ...
🌐
Educative
educative.io › answers › what-is-mathsqrt-in-javascript
What is Math.sqrt() in JavaScript?
Math is a built-in object in JavaScript that contains different methods and properties used to perform mathematical operations. It contains a sqrt() function, which is used to compute the square root of a specified number.
🌐
W3Schools
w3schools.com › js › js_math.asp
JavaScript Math Object
Math.pow(x, y) returns the value of x to the power of y: Math.pow(8, 2); Try it Yourself » · Math.sqrt(x) returns the square root of x: Math.sqrt(64); Try it Yourself » · Math.abs(x) returns the absolute (positive) value of x: Math.abs(-4.7); ...
🌐
Programiz
programiz.com › javascript › examples › square-root
JavaScript Program to Find the Square Root
To find the square root of a number in JavaScript, you can use the built-in Math.sqrt() method.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-find-the-square-root
JavaScript Program to Find the Square Root - GeeksforGeeks
July 23, 2025 - Approaches to Find the Square Root ... The square root of String Value is NaN. The Math.sqrt() method is used to find the Square Root of a given number....
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Math › sqrt
Math.sqrt() - JavaScript
\forall x \geq 0, \mathtt{Math.sqrt(x)} = \sqrt{x} = \text{the unique} \; y \geq 0 \; \text{such that} \; y^2 = x