Deborah Emeni
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    One of the major best practices in building highly functional and scalable applications is choosing a suitable and compatible runtime environment. A suitable runtime environment offers a wide range of benefits, including performance, scalability, security, and reliability. The runtime environment should be chosen based on the application's scalability requirements, programming language, and other factors. Several JavaScript runtime environments offer a wide range of features and benefits. [Node.js](https://nodejs.org/) is a popular and powerful runtime environment built on [Google's V8 JavaScript engine](https://v8.dev/). It is well-suited for building high-performing and scalable applications. [Bun.js](https://bun.sh/) is a JavaScript runtime built on a more efficient [JavaScriptCore engine](https://developer.apple.com/documentation/javascriptcore). It is designed for building applications with speed. In this article, you’ll learn the differences between Node.js and Bun.js JavaScript runtimes, including the services and features they both provide to aid in developing applications. You’ll also learn and contrast scripts written in Node.js versus Bun.js. ## Overview of Node.js Node.js is a popular open-source, cross-platform, JavaScript runtime environment that runs on the V8 JavaScript engine. It is designed to build scalable network applications. Node.js uses a single-threaded event loop architecture to handle concurrent requests efficiently. This makes it ideal for real-time applications such as chat, streaming, and single-page web applications. ### The architecture of Node.js The architecture of Node.js comprises several parts that perform various tasks at different stages, as follows: - **Requests**: First, a client sends a request to the Node.js server, which could be blocking or non-blocking. - **Event Loop**: The incoming requests are added to an event loop, which is a single thread that handles all incoming requests one by one. - **Event Emitters**: The event loop uses event emitters to notify other parts of the application when an event has occurred. - **Callbacks**: Callbacks are functions that are invoked when an event occurs. - **Non-blocking I/O**: Node.js uses non-blocking I/O to handle requests, which means that the event loop can continue to process other requests while a request is waiting for an I/O operation to complete. - **Responses**: Once a request has been processed, the event loop sends back the corresponding response to the client. You can install Node.js runtime via the [Node Package Manager (NPM)](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) by running a few commands or downloading the installer for your operating system (OS) from the [Node.js website](https://nodejs.org/en/download). ### Use cases of Node.js as a runtime environment Compared to other runtimes, Node.js stands out for its architecture and a wide variety of benefits. Some of its benefits include: - **Speed**: Node.js can efficiently process thousands of incoming requests concurrently using its event-driven, non-blocking I/O model. This is because Node.js does not block the main thread when waiting for I/O operations to complete. Instead, it fires off an event and continues to process other requests. This allows Node.js to handle a large number of requests without sacrificing performance. - **Requests processing**: Node.js uses a single-threaded event loop to achieve high performance. However, it can achieve multi-threading through clustering, which creates multiple child processes or worker threads. This allows multiple requests to be processed simultaneously without affecting the application's performance. - **Memory and resource requirements**: Node.js uses a single-threaded event loop to handle requests asynchronously, which allows it to handle more requests per second than traditional multi-threaded servers. This, in turn, improves the performance of applications and reduces the amount of memory and resources required. *Now that you have seen an overview of the Node.js runtime, you can move forward to learn about the Bun.js runtime.* ## Overview of Bun.js Bun.js is a modern, all-in-one JavaScript runtime with a native bundler, transpiler, task runner, and npm client built-in. It is zero-configuration, and it supports [TypeScript](https://www.typescriptlang.org/) out of the box. Bun.js is roughly 2-3x faster than Node.js for read queries, making it a good choice for building high-performance applications. For instance, commands run with Bun such as `bun run`, `bun install`, `bunx <command>`, and `bun test` are typically faster than Node.js commands like `npm run`, `npm install`, and `npx <command>`. Bun.js architecture is based on the JavaScriptCore engine, which is a fork of the V8 JavaScript engine. Bun.js also uses libraries built in [C](https://g.co/kgs/1tpYH8) and [Zig](https://ziglang.org/). You can install the latest version of Bun.js by running the following command: ```shell curl -fsSL https://bun.sh/install | bash ``` ### The architecture of Bun.js The architecture of Bun.js comprises of the following stages: - The user interacts with Bun through the Bun CLI. - The Bun CLI invokes the Bun Runtime. - The Bun Runtime is connected to the Bun Libraries. - The Bun Runtime provides APIs for interacting with the operating system. - Bun Apps are written in JavaScript or TypeScript and can interact with the Bun Runtime and the operating system through the Bun APIs. - The Bun CLI can be used to package and distribute Bun Apps. ### Why use Bun.js? There are several reasons to choose Bun.js. Some of these include: - **Improved performance**: Bun.js uses the JavaScriptCore engine, which is known for its speed. This results in faster startup times and runtime performance for Bun.js applications. - **Built-in TypeScript support**: Bun.js has built-in TypeScript support, so you can use TypeScript out of the box without having to transpile it. This can save you time and effort when developing applications. - **Faster development**: The built-in bundler, transpiler, and package manager in Bun.js are written in Zig, which is a very fast language. This can help to speed up the development process for your applications. ## Scripts written in Node.js versus Bun.js Let’s explore how Node.js differs from Bun.js by carrying out the following: ### Creating a server Let’s write two scripts for creating simple web servers that respond with a "Welcome to Node.js" message using Node.js and Bun.js. ### Node.js Here's a simple script creating a server in Node.js: ```javascript const http = require(‘http’); const port = 3000; const server = http.createServer((req, res) => { res.writeHead(200, {‘Content-Type’: ‘application/json’}); res.end(JSON.stringify({ message: “Welcome to Node.js” })); }); server.listen(port, () => { console.log(`Server listening on port ${port}`); }); ``` This script does the following: - Node.js uses the `http` [module](https://nodejs.org/api/http.html) to create the server. - A port number of `3000` is defined for the server to listen on. - A server object is created with a `createServer()` function that accepts a callback function as an argument. When the server receives a request, the callback function will be called, and the following happens: - The response's status code will be set to 200 (indicating a successful request). - The response's content type is set to `application/json` (indicating that the response will be a JSON object). - Then, it calls the `end()` method on the response object, which sends the response to the client. - The server is told to listen on the port number `3000` specified by the port constant. Afterwards, a message is logged to the console to indicate that the server is running. ### Bun.js Here's a simple script creating a server in Bun.js: ```javascript export default { port: 3000, fetch(request) { return new Response(“Welcome to Node.js”); }, }; ``` This script exports a default object with two properties, namely: - `port`: specifies the port number `3000` that the web server listens on. - `fetch`: This function accepts one parameter, a `request` object and returns a `response` object with the text “Welcome to Node.js”. ## Comparing the features of Node.js and Bun.js Node.js and Bun.js both differ in their core features and functionalities. The table below explores their unique strengths and weaknesses, which would enable you to determine which one is best suitable for your project's needs. <table> <tr> <th><b>Features</b></th> <th><b>Node.js</b></th> <th><b>Bun.js</b></th> </tr> <tr> <td><b>Performance</b></td> <td>Node.js leverages a high-performance V8 JavaScript engine, non-blocking I/O, a single-threaded event loop, and an asynchronous programming model, making it highly efficient for I/O-intensive tasks.</td> <td>Bun.js uses the JavaScriptCore engine, which is known for its speed, but Node.js has a larger community and more third-party libraries, which can give it a performance advantage. The performance of each framework will depend on the specific application.</td> </tr> <tr> <td><b>Community</b></td> <td>Node.js has a vibrant developer community that provides a wide range of resources, such as libraries, documentation, and tutorials.</td> <td>Bun.js is a newer runtime, so its community is not as large or active as Node.js's community. However, the Bun.js community is growing rapidly, and there are a lot of resources available for Bun.js developers.</td> </tr> <tr> <td><b>NPM support</b></td> <td>Node.js includes NPM, the default package manager for Node.js, which allows developers to install and manage modules in their projects.</td> <td>Bun.js is an npm-compatible package manager, so developers can use NPM modules in their Bun.js projects.</td> </tr> <tr> <td><b>TypeScript support</b></td> <td>Developers can run TypeScript code in Node.js by transpiling it to JavaScript using the TypeScript compiler, or by using a tool like ts-node to directly execute TypeScript code.</td> <td>Bun.js natively supports TypeScript, so developers can write and run TypeScript code without needing to transpile it to JavaScript.</td> </tr> <tr> <td><b>System requirements</b></td> <td>Node.js is compatible with a variety of systems, including both 32-bit and 64-bit architectures, and runs on major operating systems like Linux, Windows, macOS, FreeBSD, SmartOS, and AIX.</td> <td>Bun.js also requires a 64-bit processor and a supported version of Linux, Windows, or macOS.</td> </tr> </table> ## CONCLUSION This article provided an overview of Node.js and Bun.js, including their architectures and use cases. You also learned about the differences between a sample script written in Node.js and one written in Bun.js and a comparison of their features. ## Resources You may find the following resources useful: - [Node.js Documentation](https://nodejs.org/en/docs) - [Bun.js Documentation](https://bun.sh/docs)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully