Bun, the Node.js nightmare has arrived
Node.js has long been a cornerstone of the JavaScript universe, however a new challenger that should not be underestimated is entering the scene. The new one has already generated an undeniable enthusiasm around himself.
Way back in 2008, JavaScript was the coding language primarily used for building the frontend of websites and managing events. Afer a while, in 2009, Ryan Dahl introduced NodeJs, allowing developers to write JavaScript not only on the client side (frontend) but also on the server side (backend). As Node.js grew, its complexity grew with him. It was a revolutionary runtime environment, but it had many flaws, some of which persist to this day.
Wanting to solve these problems, Ryan Dahl returned in 2020 with Deno, which solved all the problems and flaws of NodeJs. However, there are common issues in both NodeJs and Deno, which still remain unresolved. To address both issues and due to frustration with the speed of NodeJs when developing a NextJs project, Jarred Summer introduced Bun in 2022.
Bun is the new rival not to be underestimated, who is generating undeniable enthusiasm around himself. The ultimate all-in-one JavaScript runtime environment and toolkit, it’s designed for speed, complete with bundler, test runner, and Node.js-compatible package manager. It can be said that it is the third of its kind after Node.js and Deno.js, the brainchild of Jarred Sumner has been created using the low-level programming language known as Zig.
Bun wants to be the evolution of the transport system which alleviates the city congestion of the metropolis which has now become Node.js, making things more fluid and simple.
A JavaScript runtime is an environment that provides all the components needed to use and run a JavaScript program.
What is Bun
Bun is a new JavaScript runtime built from the ground up to serve today’s JavaScript ecosystem. Its goal is to run most JavaScript server-side, providing the tools needed to improve performance, reduce complexity, and multiply developer productivity.
The libraries are all built in C and Zig, this is to avoid any dependency on Node or NPM, thus minimizing JavaScript in its stack, decisions aimed at maximizing the performance of the new runtime. The beauty of Bun lies in its ability to streamline the development process, so as to make it more fluid and efficient.
Its main advantages are three:
- Speed. Bun starts fast and runs fast.
By JavaScript engine we mean a program that is capable of converting JavaScript code into machine code, so as to allow the computer to perform all the required tasks.
Node.js uses Google’s V8 engine, which powers the Chrome browser. Bun instead uses JavaScriptCore (JSC), an open source JavaScript engine developed by Apple for the Safari browser.
V8 and JSC use very different architectures and optimization strategies: JSC prioritizes faster startup times and reduced memory usage with a slightly slower execution time, while V8 prioritizes fast execution with greater runtime optimization which can lead to increased memory usage. Thanks to this, Bun was 2 times faster than Deno and 4 times faster than Node. As computing moves to the edge, this is a critical point.
- Elegant APIs. Bun provides a minimal set of highly optimized APIs for performing common tasks, such as starting an HTTP server and writing files.
The JS engine, unable to work alone, combines with external APIs, message queues and the event loop to create a Javascript runtime. In the past, standard web APIs available in browsers were not natively supported, forcing developers to rely on third-party packages for this functionality.
Bun makes this simpler and more immediate by offering integrated support for standard Web APIs. Developers do not have to resort to additional third-party packages, which, in comparison, are also less fast and reliable than those offered by Bun. - Cohesive DX. Bun is more than just a runtime, it’s an advanced, comprehensive toolkit for building JavaScript apps, including a powerful package manager, test runner, and bundler.
Another important feature of Bun is that it gives the possibility to use ES and CommonJs modules together within the same file, which is impossible in Node.js.
Bundler and test runner
Bundling is the process of taking multiple JavaScript files and combining them into one or more optimized bundles.
With Node.js, grouping is usually handled by third-party tools rather than Node.js itself. Instead, Bun is designed to bundle JavaScript and TypeScript code for various platforms.
A peculiarity of Bun is the presence of JavaScript macros, which allow the execution of JavaScript functions during bundling, with the results directly integrated into the final package. This mechanism offers an alternative that could reshape the clustering landscape.
Testing is a crucial step in software development, as it ensures that the code behaves as expected, while identifying potential problems before they emerge in production. While Node.js developers rely on third parties for their testing needs, Bun integrates a test runner that ensures speed, compatibility, and a range of features suited to modern development workflows.
Bun is quickly conquering the audience. It is optimized for MacOS and Linux, with everything it offers, Bun is definitely a toolkit worth trying.