Count up to 1 billion (node ​​vs bun)

I was inspired by Ishaan Shaikh’s post on the 1 billion count: https://dev.to/sheikh_ishaan/count-to-1-billion-20de.

I wanted to see if there was a big performance difference in counting up to 1 billion in JavaScript when using node vs when using bun.

I ran this program and timed it using MacOS time utility:

for (let i = 0; i < 1_000_000_000; i++) {
}
enter fullscreen mode

exit fullscreen mode

I ran it with node and got:

node counter.js  0.46s user 0.03s system 84% cpu 0.578 total
enter fullscreen mode

exit fullscreen mode

Then, I ran it with bun and got:

bun counter.js  0.90s user 0.01s system 99% cpu 0.912 total
enter fullscreen mode

exit fullscreen mode

I was surprised to see something so simple because the time difference for bun vs node was double! Especially since Bun claimed it was faster than Node because it uses the JavaScript core instead of V8.

To verify that this was the case, I tested it on 10,000,000,000. Here were the results:

node counter.js  7.87s user 0.06s system 97% cpu 8.118 total
enter fullscreen mode

exit fullscreen mode

bun counter.js  8.79s user 0.02s system 99% cpu 8.820 total
enter fullscreen mode

exit fullscreen mode

Clearly, it looks like there might be some bunny startup/initialization cost which is why counting Node. I’m curious to know where this is coming from.

Thanks to writethrough.io for helping me write this article.

Leave a Comment