在 JavaScript 中平方一个数字的最快方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26593302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:07:04  来源:igfitidea点击:

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

javascript

提问by Bart Humphries

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

在 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/10seems to suggest that straight multiplication is faster in everything but Firefox (where both ways are similarly fast).

编辑:我看到了为什么对一个数字进行平方比将两个随机数相乘要快?这似乎表明平方比将两个随机数相乘要快,并假设 n*n 不会利用这一点,但 Math.pow(n,2) 会。正如 jfriend00 在评论中指出的那样,然后在回答中指出,http://jsperf.com/math-pow-vs-simple-multiplication/10似乎表明直接乘法在除 Firefox 之外的所有东西中都更快(两种方式同样快)。

回答by jfriend00

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.

查看此修订后的性能测试并在您关心的浏览器中运行它: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.

截至 2020 年 4 月,Chrome、Edge 和 Firefox 在上述所有三种方法之间的差异均不到 1%。

Original Answer from 2014:

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/10which compares:

在这种情况下,浏览器实现的性能差异很大。以下是此 jsperf 测试中许多不同浏览器的结果:http: //jsperf.com/math-pow-vs-simple-multiplication/10比较:

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.

更长的条形更快(更大的操作数/秒)。您可以看到 Firefox 优化了这两种操作,使其几乎相同。在其他浏览器中,乘法要快得多。IE 是最慢的,并且显示出两种方法之间的最大百分比差异。Firefox 是最快的,两者之间的差异最小。

enter image description here

在此处输入图片说明

回答by Penny Liu

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

ES6 中,您可以使用指数n ( x ** y)执行以下操作,其产生与 相同的结果Math.pow(x,y)

function squareIt(number) {
  return number ** 2;
}

console.log(squareIt(5));

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

或者你可以使用一个名为BigInteger.js的 JavaScript 库来达到这个目的。

alert(bigInt(5).square());
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.40/BigInteger.min.js"></script>