Making Faster Javascript — BUN Intended!

Making Faster Javascript — BUN Intended!!!

BUN JS

​​

Evolution of JS and Tools

JavaScript was invented by Brendan Eich in 1995. It was developed for Netscape 2, and became the ECMA-262 standard in 1997. Nodejs released in 2009. By this time JavaScript was a more serious language, after ‘Web 2.0’ applications such as Gmail showed the world what a modern experience on the web could be like.

JS engines became better. All major browsers started providing better support for JavaScript and strived to provide better performance. Node is using V8 engine (Chrome V8) which is one of the best till date.

We started building large projects and felt the need to solve our bundling issues. And we welcomed Webpack in 2012. Now one less thing to worry about.

We started talking about polyfills. We knew indexOf works across the browsers but includes doesn’t. Writing polyfills in interviews became a trend. And then we welcomed babel in 2014. Fast forward to 2022, polyfill is obsolete. I already feel old because of this!

Not everything is about JS! We want to be fast!

Right when we were beginning to feel like JS God(sometimes being lame is cool!), tools like webpack and babel started to feel the heat! There were competitors.

Languages like Rust and Go got introduced. They proved to be more performant than JavaScript for bundling, transpiling and compiling processes. They are multithreaded and can handle a large number of files. These tools run on developer machines. They don’t need to be using JS. Cool! Can we go faster?

Welcome Bun!

BUN Logo

JS is fast. No pun intended!! Hmm..maybe or maybe not. Depends. But we do love it when something new is introduced in our ‘small’ JS world.

Bun intended!!

We want fast…more fast…blazing fast!!!

We want fast…more fast…blazing fast!!!

Year 2022, Jarred Summer just announced its beta release and claims that it’s significantly faster than node.

Faster? How much? Orders of magnitude faster!! Post covid scenes are definitely going to be significantly different. :)

BUN claims exceptional performance

Bun claims exceptional performance

It’s always a good idea to be a little sceptical about the benchmarks provided by the creator, but as we can see above, it’s almost 3 times faster when server rendering React, running database queries or running native code. Definitely impressive!!

Source

We know about Google’s V8, one of the most powerful JS engines, which powers chrome, node etc and makes JavaScript extremely fast with just in time compilation. Now we have a new player Bun which looks very promising for one obvious reason, performance!

What makes it fast?

SOURCE

Bun doesn’t use the V8 engine

Source: WEBKIT

It uses JavaScript core from webkit which is generally considered to be faster but more difficult to work with. In addition it’s written in a low level programming language called Zig. Zig is a relatively new language that’s similar to C or rust.

As the creators of Bun says:

An enormous amount of time spent profiling, benchmarking and optimising things. The answer is different for every part of Bun, but one general theme: Zig’s low-level control over memory and lack of hidden control flow makes it much simpler to write fast software.

What does it offer?

Source

It’s fun to go fast! But more importantly Bun is an all-in-one runtime.

Native Bundler: It has a native bundler to replace tools like WebPack and also has a native transpiler so you can write TypeScript code out of the box with cool features like top level await.

Transpile: Bun will also transpile JSX files. Also, like Dino, it prioritises web api’s like fetch and also supports many node core modules as well as node apis which will allow many node packages to also run in Bun.

Node Packages: It implements node’s module resolution algorithm, which means, you can install packages from npm into Bun and those packages install 20 times faster. Magic!!

They say:

‘All in one fast & easy-to-use tool. Instead of 1,000 node_modules for development, you only need a bun.’

Environment Variables: Another feature is that environment variables load automatically. So, .env need not be installed into every project.

Test Runner: It also comes with its own test runner similar to Jest. And again it’s fast. Blazingly fast!

Conclusion

It’s still experimental! It’s beta. And we can expect bugs. Even if this tool is not yet production-ready it is awesome that such new tools and the metrics are coming up. Oh man, causes an adrenaline rush!

Also, Bun is not an all around tool. Do check out https://github.com/Jarred-Sumner/bun and you can find a link to check the things that don’t work yet.

What’s next? Let’s get our hands dirty and feel the power.

Making Faster Javascript — BUN Intended!!! was originally published in Walmart Global Tech Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

Article Link: Making Faster Javascript — BUN Intended!!! | by Manish Singh | Walmart Global Tech Blog | Medium