最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If you try 9n**9n**9n in Chrome's console, Chrome breaks (it resembles an infinite loop). Why does this hap

programmeradmin1浏览0评论

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

  • Does the V8 engine lack the implementation for this case?

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

  • Why doesn't V8 return Infinity as well in the former case?
  • And why does it seem to go into an infinite loop?

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

Thanks!

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

  • Does the V8 engine lack the implementation for this case?

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

  • Why doesn't V8 return Infinity as well in the former case?
  • And why does it seem to go into an infinite loop?

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

Thanks!

Share Improve this question edited Nov 28, 2018 at 14:33 Boann 50.1k16 gold badges124 silver badges152 bronze badges asked Nov 28, 2018 at 10:44 TiberiuTiberiu 4681 gold badge3 silver badges14 bronze badges 5
  • 2 Just a question, what is 9n**9n? Is n any number? Just for clarification. – lloydaf Commented Nov 28, 2018 at 10:50
  • 4 "A BigInt is created by appending n to the end of an integer literal — 10n — or by calling the function BigInt()." This is from MDN developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Tiberiu Commented Nov 28, 2018 at 10:53
  • I use Edge only for reading epubs on my puter. It's very nice. – Tiberiu Commented Nov 28, 2018 at 10:59
  • 1 seems like chrome can go up to 9n**9n**5n - but the length of that number is also 56349 characters long.. – Stender Commented Nov 28, 2018 at 11:03
  • Yes, it is. I use Edge on my mobile phone over Chrome and Firefox, but for a debugging console I use Firefox. – Mahab Commented Nov 28, 2018 at 11:11
Add a ment  | 

3 Answers 3

Reset to default 10

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

On my system this bees 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).

Update:

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;

gives:

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

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

Background information:

In JavaScript you can use n suffix for making the number as bigint (simpy big numbers). Big numbers have different ways of putations. Usually they are more "expensive" to pute. It does not use the processors built-in methods for calculations. Instead bigints use soft putations.

Description of the issue:

9n**9n means 9^9 (9*9*9*9*9*9*9*9*9). It is 387420489. Even if you multiply 387420489 by itself it is really big number. But **9n means you want to pute 387420489^9 which is really really very big number. It seems for chrome it takes too long to calculate it or some unknown issue happens.

Basically it must be a bug which needs to be reported. Freezing the browser in weird way is not good user experience.

The answer as to why 9**9**9 returns Infinity is because it doesn't take very long to overflow the maximum value (around 2^1024). It might even shortcut it with the ** as see if the first number >= 2 and the second number > 1024, then it will be Infinity.

With BigInt, it can represent much, much larger numbers, so that's what it tries to do. It takes a long time to reach "Infinity" with BigInt (EDIT: which would actually be a RangeError exception). Finding 387420489 can be done pretty quick, but 9n**387420489n, where it is multiplying BigInts almost 400 Million times...that takes a while.

A BigInt operation is much slower than a regular int operation. I expect you might get a result (or a RangeError exception) if you wait 20-30 minutes, but could be much longer.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论