如果您在Chrome的控制台中尝试9n ** 9n ** 9n,Chrome会中断(类似于无限循环).为什么会这样? [英] If you try 9n**9n**9n in Chrome's console, Chrome breaks (it resembles an infinite loop). Why does this happen?

查看:138
本文介绍了如果您在Chrome的控制台中尝试9n ** 9n ** 9n,Chrome会中断(类似于无限循环).为什么会这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在Chrome的控制台中尝试9n**9n**9n,Chrome会中断(类似于无限循环).

If you try 9n**9n**9n in Chrome's console, Chrome breaks (it resembles an infinite loop).

  • 在这种情况下,V8引擎是否缺少实现?

我的意思是,如果您尝试使用9**9**9,它将返回Infinity,这很好.

I mean, if you try 9**9**9 it will return Infinity, which is kind of nice.

  • 为什么在前一种情况下V8也不会返回Infinity?
  • 为什么它似乎陷入无限循环?

我也在Firefox中尝试过此操作,并且此问题不存在,因为当前SpiderMonkey中没有BigInt实现.

I tried this in Firefox too, and this problem doesn't exist, because currently there's no BigInt implementation in SpiderMonkey.

谢谢!

推荐答案

如前所述,9n9的BigInt表示形式.

As was said already, 9n is the BigInt representation of 9.

**(幂)运算符从右到左工作,从而导致结果快速升级:

The ** (power) operator works from right to left, causing quick escalation of results:

2n**2n**2n === 2n ** 4n === 16n
3n**3n**3n === 3n ** 27n === 7625597484987n
4n**4n**4n === 4n ** 256n === 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096n

在我的系统上,这从7n**7n**7n开始变得相当缓慢,计算打印大约需要32秒.结果为695976位,其中前5000位显示在控制台中.

On my system this becomes quite laggy from 7n**7n**7n, which takes about 32 seconds to calculate print. The result is 695976 digits, the first 5000 of which are printed in the console.

我没有再尝试过,但是我只是说它只是在考虑结果. 计算打印可能要花费数小时或数天的时间(或者有时可能会出现内存不足的情况).

I haven't tried it any further, but I'd say it is just chewing away on the result. This could well take hours or days to calculate print (or perhaps even an Out Of Memory situation might occur at some point).

更新:

我刚刚在Chrome控制台中尝试了var x = 7n**7n**7n,因此只需将其分配给一个变量即可完成此操作.事实证明, 将bigint转换为字符串 是很费时间的.打印 x.toString().length 所需的时间与打印x7n**7n**7n相似.

I just tried var x = 7n**7n**7n in the Chrome console, so just assigning it to a variable, and this finished in almost no time. It turns out that converting the bigint to a string is what takes up time; printing x.toString().length takes a similar amount of time as printing x or 7n**7n**7n.

进一步的实验揭示了其他有趣的行为,请参见以下结果:

Further experimenting revealed other interesting behaviour, see these results:

// Pure calculation time increases significantly when the exponent grows:
var x = 7n**7n**7n; // ~   1200 ms
var x = 7n**8n**7n; // ~   7000 ms
var x = 7n**7n**8n; // ~  62000 ms
var x = 7n**8n**8n; // ~ 470000 ms

// But it's a different story when the base number is 'simple' in binary terms, e.g. 8n:
var x = 8n**7n**7n; // ~      1 ms
var x = 8n**8n**7n; // ~      1 ms
var x = 8n**7n**8n; // ~      7 ms
var x = 8n**8n**8n; // ~     17 ms

是的,这一切都结束了.

And yes, there is an end to it all:

var x = 32n**16n**8n;

给予:

VM436:1 Uncaught RangeError: Maximum BigInt size exceeded
at <anonymous>:1:28

Chrome的上限似乎为 10亿位(1e9位),或大约125 MB-参考:

The upper limit in Chrome appears to be 1 billion bits (1e9 bits), or about 125 MB - Reference: https://github.com/tc39/proposal-bigint/issues/174#issuecomment-437471065

这篇关于如果您在Chrome的控制台中尝试9n ** 9n ** 9n,Chrome会中断(类似于无限循环).为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆