![cover-img](https://project-assets.showwcase.com/1420x/63285/1696784414868-Cover%2520img(1).png?type=webp)
Bun Vs Node.js : A Deep dive
8 October, 2023
4
4
3
Contributors
Want to try a new tool for running Javascript? Bun says it's better than Node.js. This article will help you see if that's true by comparing their performance.
📋Prerequisites: What you need before diving in
Before starting this guide, it helps if you know some basic Javascript and how to make simple web apps.
Make sure you have:
- Node 18 or a newer version
- Bun set up
- npm ready to go
- A tool to write code(code editor)
🔍 Overview: A bird's-eye view of our discussion
In this guide, we'll check out Bun, a new tool that everyone's talking about. We'll learn what it does and why some people are starting to use it. We'll also carry out some benchmark test to see if Bun is as fast as its makers claim. Let's get started!
🤔 Understanding Runtimes
Imagine you have a box of LEGO and want to build a spaceship. Think of the instructions as the plan, and you need a helper to put it all together. That helper is like a runtime!
When you show the LEGO plan to the helper, they start building, following each step carefully. They make sure all the bricks fit and, when done, they also help the spaceship "fly".
In computer terms, a runtime is a helper for computer programs. It reads a program's steps and makes sure it works right. So, like the LEGO helper builds the spaceship, the runtime helps run computer programs.
The Javascript runtime
The JavaScript runtime is like a bridge between JavaScript and the computer. When you ask JavaScript to do a task, the runtime helps the computer understand it.
Think of the runtime as a helper. It ensures JavaScript works properly and doesn't break any rules, kind of like how a teacher makes sure students do their tasks right.
Once the runtime gets an answer from the computer, it gives it back to JavaScript. Then, JavaScript can display it or use it in different ways, like showing something on your screen.
🍞 Diving into Bun: Intro to our main topic
Bun is a new tool for running JavaScript, made using a cool language called Zig. Its main goal is to run JavaScript really fast and make life easier for developers.
Imagine you tell Bun a set of instructions, like guiding a super-fast car. Bun doesn't just get where you want to go, it gets there faster than most!
Zig, the language used to create Bun, is like a secret sauce making everything efficient. Using Bun, you can create websites or apps that are super speedy. Bun also has a tool, called CLI, that helps you manage your project and run your codes easily.
🚀 The Speed of Bun: Why's it so zippy?
Bun runs super fast, and here's why:
- Simple Design: Bun is made to be simple and use fewer resources. This means it can work quickly and use less memory than other similar tools.
- Smart Coding: Bun is made using Zig, a newer way to code that lets developers have more control, especially with memory. This makes Bun efficient.
- Using a Fast Engine: Instead of using the common V8 engine, Bun uses JavaScriptCore from WebKit, known for its speed. This helps Bun run JavaScript very fast.
- All-in-One Tools: Bun has its own set of built-in tools that makes building things easier. For example, you don't need outside tools like Webpack because Bun already has a bundler. It also lets you use TypeScript directly and has a testing tool similar to Jest. Plus, there's no need for extra packages to manage environment settings.
💻 Setting Up the Bun Runtime: Step-by-step guide
To install Bun, You can follow these steps:
- Open the terminal or command prompt on your computer.
- Type in and enter:
curl -fsSL https://bun.sh/install | bash
- If you're using a Mac, type this next:
exec /bin/zsh
- After pressing Enter, the system will start downloading Bun from its official site and install it for you. Just wait a bit!
- Once it's done, Bun is all set on your computer.
Now, you can use Bun for your JavaScript and TypeScript projects and enjoy its fast performance and cool features.
🤷♂️ Bun vs. Node: Who wins?
Let's see how Bun and Node match up against each other:
- Speed: Bun is built for quick startups and runs using the JavaScriptCore engine from WebKit, known for being speedy. Node.js uses the V8 engine, which is also fast, but there might be slight differences in speed.
- Size & Extras: Bun is made to be simple with fewer extras. It has tools like a bundler built right in, so you don't need many outside tools. Node.js is bigger with more features, but you might need to add more extras for specific tasks.
- Working Together: Bun wants to be a replacement for Node.js, but there might be a few bumps when trying to use them the same way. Bun tries to cover most of what Node.js does, but there may be a few gaps.
- Tools: Bun gives you everything in one package, like a bundler and package manager. Node.js, however, has lots of outside tools and libraries you can pick from, like Webpack or npm.
- Community: Node.js has been around longer, so it has a big community and lots of resources. Bun is newer, so its community might be smaller for now.
In short, both have their strengths, and what you choose might depend on what you need for your project.
⏱ Putting competitor to the Test: All about benchmarks
I'm running a benchmark test on my Lenovo Ideapad with 8GB RAM. We'll use the k6 tool by Grafana labs for this test. If you need to install the tool, there's a guide for it. Here's what software versions I have on my computer:
- Node v18.18.0
- Bun v1.0.2
I took some basic HTTP server code examples from the official sites of Bun and Node.js. Let's look at the "Hello World" code for both Bun and Node.
Node
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Run the command in your terminal:
node nodeserver.js
Your node server will be running on port: http://localhost:3000/
Bun
const server = Bun.serve({
port: 3001,
fetch(req) {
return new Response("Hello World");
},
});
console.log(`Listening on http://localhost:${server.port}`);
Your Bun server will be running on port: http://localhost:3001/
Run the command in your terminal:
bun run bunserver.js
Create a script.js
file and paste this test script:
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
http.get('http://localhost:3000/'); // this will change depending on the server you're testing for
sleep(1);
}
In your terminal run:
k6 run script.js
Here is the result for our Node server:
Here is the result for our Bun server:
📝 Final Thoughts: Wrapping things up
Bun and Node.js are like two different toolkits for building with JavaScript. Bun is quick, simple, and has everything in one place. It runs on WebKit's engine to make things fast. Node.js, however, has more tools and a bigger community, working on the V8 engine. Which one to pick? It depends on your project's needs and what you're comfortable with. Both are great for creating cool JavaScript stuff.
This is the submission for the #writetowin challenge for the track of development
Thank you ShowwcaseHQ for this awesome event.
showwcase
writetowin