Node.js Vs Bun Comparison
Madhur Rastogi
Node JS |?| JavaScript |?| Git |?| ElasticSearch |?| MongoDB |?| SQL |?| NEXTJS |?| HTML |?| GraphQL |?|
Hi, I Hope you are doing great I want to share my thoughts on the comparison of Node.js vs Bun, recently I built a practice project on Bun to understand more deeply how Bun is different works from NodeJS, so I divided it into some Parts of Comparison.
Before going into deep, I share a little bit of background history of Node.js and Bun, I know smart developers already know about it??.
NodeJS:
Node.js is an asynchronous event-driven open-source JavaScript runtime written with C++. Node.js's first release was launched on 27 May 2009.
Bun:
Bun is an open-source JavaScript runtime, Package manager, bundler, and test runner, and it is written in Zig. Bun's first release was on 08 September 2023.
I believe you haven't heard so much about Zig, but Zig is a Programming Language to focuses on simplicity, performance, optimal, and safety, Even I have not heard about it so much????.
Let's start to understand the Node.js " V??S" Bun.
Beginning to start with Setup in Node.js Vs Bun??
It's not so much different to initialize the project.
Node.js
npm init
npm init is used to set up a new or existing npm package.
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (nodedemo)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /mac/madhurrastogi/linkedin/comparision/nodevsbun/package.json:
{
"name": "nodedome",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
The NPM init command creates a package.json file.
I share one more cool thing about package.json and package-lock.json, Do you guys know what is role of package-lock.json is in Node.js?
I know you guys are curious why I changed the topic, but don't worry, I know the topic is comparison ????. I'm just sharing my findings.
"package.json is a metadata file for a Node.js project. It typically contains information such as project name, version, description, scripts, dependencies, and other metadata. It includes high-level dependency information but doesn't specify the exact version of dependencies. For that purpose, the package-lock.json file comes into play to help lock down the specific version of every package and its sub-dependencies. If you note the minor difference, there is a "^" caret in package-lock.json you never find this symbol. package-lock.json ensures that every developer working on the project or deploying it gets the same dependency tree. Therefore, package-lock.json plays the most important role in our Node.js project."
Now coming back to Bun initialize setup
Bun
$ bun init
bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit
package name (bunInit):
entry point (index.ts):
Done! A package.json file was saved in the current directory.
+ index.ts
+ .gitignore
+ tsconfig.json (for editor auto-complete)
+ README.md
To get started, run:
bun run index.ts
Bun init creates node_modules, bun.lockb, tsconfig.json, package.json, and adds bun-types package.
领英推荐
├── bun.lockb
├── index.ts
├── node_modules
│ └── bun-types
│ ├── package.json
│ ├── README.md
│ ├── tsconfig.json
│ └── types.d.ts
├── package.json
├── README.md
└── tsconfig.json
2 directories, 9 files
JavaScript XML and TypeScript
To enable TypeScript and JavaScript XML in Node.js, you must install TypeScript, ts-node, ReactJS, and React-DOM. On the other hand, in Bun, everything is pre-configured to compile your project.
"Notably, when running a React.js application in Bun, it's essential to install ReactJS first."
Common JS & ECMAScript Modules compatibility
In Bun, you can use both ESM and CommonJS modules in one file without needing to configure anything. However, in Node.js, you cannot use both in one file. If you use CommonJS, you need to add 'type: commonjs' to your package.json configuration.
Web APIs
Web APIs play a crucial role in every project. In Node.js v17, the fetch function is not supported, so calling APIs requires third-party packages like Axios or node-fetch. However, with the recent release of Node.js v18 LTS, an in-built fetch function has been provided.
Similarly, Bun also offers in-built web APIs, with most of them being stable and ready for production use.
// Fetch Bun web API: https://developer.mozilla.org/en-US/docs/Web/API/fetch
const response = await fetch("https://dummyjson.com/posts?limit=10");
// Response Bun web API: https://developer.mozilla.org/en-US/docs/Web/API/Response
const result = await response.json();
// Console Bun web API: https://developer.mozilla.org/en-US/docs/Web/API/console
console.log(result.posts)
Hot Reloading
I believe you've heard about different terms. I am clear on what hot reloading is; essentially, it helps to reload your application in the browser when any changes occur in your project files.
In Bun, it is a built-in feature, so you do not need to install any package. You can use the '--hot' flag in the running script to enable hot reloading. However, in Node.js, you can use "nodemon", a third-party package, to enable hot reloading.
Test Runner
The test case is crucial when developing a project. It helps ensure that the code is compatible with various errors and functions correctly or not.
Bun comes with an inbuilt test runner, eliminating the need to install any third-party package for testing. It functions similarly to the Jest library. However, in Node.js, you need to install third-party packages for testing and set up configuration.
Conclusion
Both languages have their own valuable importance. The only thing I noticed is that Bun helps reduce configuration for your project; you do not need to depend on any third-party package.
In Node.js, when using any modern tool like TypeScript or a package manager, you need to depend on third-party packages, and in some cases, you need to write configuration.
You can share and follow me on LinkedIn. Hope you Like it.??????????
Full Stack Developer
7 个月Very good Comparison ???