Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to square a number in JavaScript?

Tags:

javascript

What's the fastest way to square a number in JavaScript?

function squareIt(number) {    return Math.pow(number,2); }  function squareIt(number) {    return number * number; } 

Or some other method that I don't know about. I'm not looking for a golfed answer, but the answer that's likely to be shortest in the compiler, on the average.

Edit: I saw Why is squaring a number faster than multiplying two random numbers? which seemed to indicate that squaring is faster than multiplying two random numbers, and presumed that n*n wouldn't take advantage of this but that Math.pow(n,2) would. As jfriend00 pointed out in the comments, and then later in an answer, http://jsperf.com/math-pow-vs-simple-multiplication/10 seems to suggest that straight multiplication is faster in everything but Firefox (where both ways are similarly fast).

like image 848
Bart Humphries Avatar asked Oct 27 '14 17:10

Bart Humphries


People also ask

How do I square a number in JavaScript?

By using the Math. pow() method, you can easily find the square of a number by passing the number 2 as your exponent parameter. And that's how you find the square of a number using Math.

Is there a trick to squaring numbers?

Step 1: Add the last digit of the number you are trying to square to the entire number itself, creating your sum. Step 2: Multiply the sum (step 1) by the first digit of the base number. Step 3: Square the last digit of the base number.


2 Answers

Note: Questions like this change over time as browser engines change how their optimizations work. For a recent look comparing:

Math.pow(x1, 2) x1 * x1 x1 ** 2                  // ES6 syntax 

See this revised performance test and run it in the browsers you care about: https://jsperf.com/math-pow-vs-simple-multiplication/32.

As of April 2020, Chrome, Edge and Firefox show less than 1% difference between all three of the above methods.

If the jsperf link is not working (it seems to be occasionally down), then you can try this perf.link test case.

Original Answer from 2014:

All performance questions should be answered by measurement because specifics of the browser implementation and the particular scenario you care about are often what determine the outcome (thus a theoretical discussion is not always right).

In this case, performance varies greatly by browser implementation. Here are are results from a number of different browsers in this jsperf test: http://jsperf.com/math-pow-vs-simple-multiplication/10 which compares:

Math.pow(x1, 2) x1 * x1 

Longer bars are faster (greater ops/sec). You can see that Firefox optimizes both operations to be pretty much the same. In other browsers, the multiplication is significantly faster. IE is both the slowest and shows the greatest percentage difference between the two methods. Firefox is the fastest and shows the least difference between the two.

enter image description here

like image 83
jfriend00 Avatar answered Oct 16 '22 08:10

jfriend00


In ES6 you can do the following with Exponentiation (x ** y), which produces the same result as Math.pow(x,y):

function squareIt(number) {    return number ** 2;  }    console.log(squareIt(5));

or you can use a JavaScript library called BigInteger.js for the purpose.

alert(bigInt(5).square());
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.40/BigInteger.min.js"></script>
like image 36
Penny Liu Avatar answered Oct 16 '22 10:10

Penny Liu