Here is a small improvement I can suggest. First - start iterating from 0. Second - exit loop when the square of root candidate exceeds the number.

function squareroot(number) {
    for (var i = 0; i * i <= number; i++) {
        if (i * i === number)
            return i;
   }
   return number; // don't know if you should have this line in case nothing found
}

This algo will work in O(√number) time comparing to initial O(n) which is indeed performance improvement that you asked.

Edit #1

Just even more efficient solution would be to binary search the answer as @Spektre suggested. It is known that x2 is increasing function.

function squareroot(number) {
    var lo = 0, hi = number;
    while(lo <= hi) {
         var mid = Math.floor((lo + hi) / 2);
         if(mid * mid > number) hi = mid - 1;
         else lo = mid + 1;
    }
    return hi;
}

This algo has O(log(number)) running time complexity.

Answer from Ivan Gritsenko on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › square-root-function-without-using-math-sqrt-in-javascript
Square root function without using Math.sqrt() in JavaScript
const square = (n, i, j) => { let mid = (i + j) / 2; let mul = mid * mid; if ((mul === n) || (Math.abs(mul - n) < 0.00001)){ return mid; }else if (mul < n){ return square(n, mid, j); }else{ return square(n, i, mid); } } // Function to find the square root of n const findSqrt = num => { let i = 1; const found = false; while (!found){ // If n is a perfect square if (i * i === num){ return i; }else if (i * i > num){ let res = square(num, i - 1, i); return res; }; i++; } } console.log(findSqrt(33));
People also ask

Is there a way to calculate square roots in JavaScript without Math.sqrt()?
Yes, you can manually implement a square root calculation using methods like the Babylonian method (also known as Heron's method). This involves an iterative approach to approximate the square root.
🌐
wscubetech.com
wscubetech.com › blog › square-root-in-javascript
How to Find Square Root in JavaScript? With & Without ...
What is Math.sqrt() in JavaScript?
Math.sqrt() is a built-in JavaScript function that is used to calculate the square root of a given number. It is a part of the Math object in JavaScript, which provides a collection of mathematical constants and functions.
🌐
wscubetech.com
wscubetech.com › blog › square-root-in-javascript
How to Find Square Root in JavaScript? With & Without ...
Why would you manually calculate a square root instead of using Math.sqrt()?
Manually calculating a square root can be educational, helping you understand the underlying algorithm and improving your programming skills. It's also useful in environments where you might need a custom implementation for specific accuracy requirements or when dealing with complex numbers.
🌐
wscubetech.com
wscubetech.com › blog › square-root-in-javascript
How to Find Square Root in JavaScript? With & Without ...
🌐
Reddit
reddit.com › r/javascript › how to implement sqrt() in javascript.
r/javascript on Reddit: How to implement sqrt() in javascript.
January 26, 2011 - No it doesn't, it tests whether they understand the underlying mathematics of the square root function, and then whether they can implement a binary search....assuming they choose that particular solution to this problem with many possibilities.
🌐
TutorialsPoint
tutorialspoint.com › home › articles on trending technologies › finding square root of a number without using math.sqrt() in javascript
Finding square root of a number without using Math.sqrt() in JavaScript
December 11, 2020 - We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return the square root of the number provided as the input.
🌐
EyeHunts
tutorial.eyehunts.com › home › square root javascript without math | example code
Square root JavaScript without math | Example code
February 2, 2022 - <!DOCTYPE html> <html> <head> <script> function sqrt(a) { var x, x1 = a / 2; do { x = x1; x1 = (x + (a / x)) / 2; } while (x !== x1); return x; } console.log(sqrt (2)); console.log(sqrt (9)); console.log(sqrt (25)); </script> </head> </html> ...
Find elsewhere
🌐
WsCube Tech
wscubetech.com › blog › square-root-in-javascript
How to Find Square Root in JavaScript? With & Without ...
January 20, 2026 - Discover the easiest ways to find square roots in JavaScript, both with and without using built-in functions. Learn now!
🌐
TutorialsPoint
tutorialspoint.com › How-to-get-the-square-root-of-a-number-in-JavaScript
How to get the square root of a number in JavaScript?
In the above output, users can see that the square_root method returns the desired square root value for the positive integer and the negative and non-numeric values. We have learned how to get the square root value of a number using JavaScript with and without Math.sqrt() method.
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 !!!

🌐
WsCube Tech
wscubetech.com › resources › javascript › programs › square-root
JavaScript Program to Find the Square Root: With Examples
January 20, 2026 - Discover the easiest ways to find square roots in JavaScript, both with and without using built-in functions. Learn now!
🌐
FrontendLead
frontendlead.com › home › coding questions › math sqrtroot
How to Calculate Square Roots in JavaScript - FrontendLead
February 26, 2024 - Learn an efficient method to calculate the integer part of square roots in JavaScript using a binary search algorithm. Perfect for developers looking for alternatives to Math.sqrt and those interested in enhancing their algorithmic problem-solving skills.
🌐
W3Schools
w3schools.com › jsref › jsref_sqrt.asp
JavaScript Math sqrt() Method
The Math.sqrt() method returns the square root of a number. The Math.cbrt() Method The Math.SQRT2 Property The Math.SQRT1_2 Property ... Math. is an ECMAScript1 (JavaScript 1997) feature.
🌐
Codecademy
codecademy.com › forum_questions › 51033f46ee474b8a97001478
Is there a way to make a square root command? | Codecademy
I was trying to create a function that I could use to calculate the quadratic formula in my scratch pad but I could not find a command that would find the square root of a number. I tried sqrt(); and Math.sqrt(); like it was posted on another javascript website; however, I always got back an error. ... As we can see, you are using the right Math object method. Don’t know what could be wrong without seeing your code.
🌐
Quora
quora.com › How-can-I-get-square-root-of-numbers-in-Javascript
How to get square root of numbers in Javascript - Quora
Answer (1 of 3): There are multiple ways of doing that either you can create a function who does it for you or you can use a built-in function provided by javascript using”Math" object. Aproach 1: [code]function sqrt(a){ return a**(0.5); } [/code]Approach 2: [code]Math.sqrt(a); [/code]Approa...
🌐
TutorialsPoint
tutorialspoint.com › finding-square-root-of-a-non-negative-number-without-using-math-sqrt-javascript
Finding square root of a non-negative number without using Math.sqrt() JavaScript
We will make use of the binary search algorithm to converse to the square root of the given number. ... const squareRoot = (num = 1) => { let l = 0; let r = num; while(l <= r) { const mid = Math.floor((l + r) / 2); if(mid ** 2 === num){ return mid; }else if(mid ** 2 > num){ r = mid - 1; } else{ ...