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


What is the fastest way to jot down a number in javascript?

  function square (number) {return monastery pow (number, 2); } Function square (number) {return number * number; }  

Or I do not know about any other method I am not looking for a golf answer, but on average, the answer that gives the least answer in the compiler.

EDIT: I saw that it was visible that two random numbers of squaring, and estimated that n * n would not take advantage of this, but MathPo (N, 2) would be as jfriend00 did in the comments Told, and later in the north, it seems to suggest that straightening product is everything fast in Firefox (both ways are equally fast).

All display questions should be used to measure the answer because the browser implementation and special scenario that you care about Often determine the result (thus a theoretical discussion is not always true).

In this case, performance by browser implementation varies greatly. This JSPR is the result of many different browsers: which compares:

  Math.p. ( X1, 2) x1 * x1  

Longer times are faster (more ops / seconds) You can see that optimizing both Firefox functions is very similar in other browsers, Multiplication is quite fast. IE is both slow and shows the largest percentage difference between the two methods. Firefox is the fastest and shows the least difference between the two.

Enter image details here


Comments