What's Bun's Deal?
Bun's the new hot-shot JavaScript/Typescript runtime. But what's the deal with it? Why should you care? Let's find out!
Tags
TLDR🥱!!!
- Yeah Bun’s really awesome and it was an amazing dev-xp too 😳.
- But do not count out NodeJs just yet ⚠️.
Installing Bun 📦
Just go checkout the docs for this one 😒, Bun’s website
Bun in the Oven 🥖
So I’m using Bun v1.22.0
here and I’m using it all on WSL-2, Ubuntu 22
on Windows 11.
And I initialized a new Bun project with bun init
(just like the docs say)
Zoom In & Out using scroll wheel, Reset by double clicking
Okay now, lets just quickly test if things are working alright :-
(and let’s not forget that little ‘n’ in there now 🫣)
And the stuff was working fine and dandy, so let’s get to the good stuff now.
Benchmarking 📊
The code that I used this👇🏻 and it’s just generating a bunch of fibonacci recursively and writing them to a file.
const timeStart = new Date().getTime(); // start timer function fibRecursive(n: number): number { return n <= 1 ? n : fibRecursive(n - 1) + fibRecursive(n - 2); } function generateFibNumbers(count: number): number[] { const fibNumbers: number[] = []; for (let i = 0; i < count; i++) { fibNumbers.push(fibRecursive(i)); } return fibNumbers; } const fibs = generateFibNumbers(40); // write to file (Bun Specific), for node just use fs Bun.write("fibs.json", JSON.stringify(fibs, null, 2)); const timeEnd = new Date().getTime(); // end timer const diff = timeEnd - timeStart; console.log(`Done in ${diff}ms`);
Two things to note here 👇🏻
- I know that this isn’t some fancy benchmarking code and I’m not using some fancy library here 😒, but it’s just a simple test to see how Bun compares to NodeJs.
- All I’m saying is that if we run the exact same code on the same machine for both the runtimes, we can get a pretty good idea of how they compare.
Dem Digits 📈
So ran it a bunch of times with Bun and NodeJs and here’s what I got :-
- Bun’s Average Time - (853 + 875 + … + 846)/8 = 868.25ms
- Node’s Average Time - (1376 + 1352 + … + 1370)/7 = 1360.57ms
So Bun’s about 1.5x faster
than NodeJs on my machine for spitting out 40 fibonacci numbers and writing them to a JSON file.
So What’s Bun’s Deal? 🤔
So what are those numbers you see on Bun’s website, where they say that Bun’s just crazy fracking fast?
Well, they’re not lying, but they’re talking about serving HTTP requests and not just crunching numbers and simple file IO.
If you’re thinking of abandoing your Electron app (which uses NodeJs on the main thread for all file system related things) for something that runs on Bun which might have half baked support compared to NodeJs, then you might want to think again.
Node’s fine for now, but Bun’s definitely something to keep an eye out for in the future 👀.
Proof of dem digits 👇🏻
That’s it!, thanks 🤗 for reading all the way till down here