oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.4k stars 2.78k forks source link

Huge bigints work both in Deno and Node, but not in Bun #15072

Open stepaniukm opened 2 weeks ago

stepaniukm commented 2 weeks ago

What version of Bun is running?

1.1.34+5e5e7c60f

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

Given the following function for calculating fib numbers

export function fib(n: number) {
  const numbers = [0n, 1n];
  const indexes = [0, 1, 2];

  for (let i = 3; i <= n; i++) {
    numbers[indexes[2]] = numbers[indexes[1]] + numbers[indexes[0]];
    for (let j = 0; j < indexes.length; j++) {
      indexes[j] = (indexes[j] + 1) % 3;
    }
  }

  return numbers[(n - 1) % 3];
}

When testing just for fun, in Bun already around console.log(fib(1_750_000)); on my machine I started getting error:

1 | export function fib(n: number) {
2 |   const numbers = [0n, 1n];
3 |   const indexes = [0, 1, 2];
4 |
5 |   for (let i = 3; i <= n; i++) {
6 |     numbers[indexes[2]] = numbers[indexes[1]] + numbers[indexes[0]];
                              ^
RangeError: Out of memory: BigInt generated from this operation is too big

But, the exact same code works both in Node and Deno, so as per your approach, you consider that as a bug in Bun. I've tested the code in Deno and Node, and on my machine, I could easily go up to n=3_000_000 and it worked fine. Probably the limits in both of these runtimes are even higher, but that I think is enough already for submitting this issue.

What is the expected behavior?

I expect Bun, to work the same as Node and Deno, and give me correct output.

What do you see instead?

Above described RangeError

Additional information

Other runtimes used for testing:

190n commented 1 week ago

Bun uses JavaScriptCore which limits BigInt to 2^20 - 1 bits (~1 million), while Node and Deno use V8 which seems to set the limit at 2^30 - 1 (~1 billion). We can investigate how hard it is to increase JSC's limit.