Node.js正在消亡!Bun 1.0正在改变JavaScript游戏

Node.js正在消亡!Bun 1.0正在改变JavaScript游戏

Tags
bun
Node.js
Published
Author
notion image
Before going further, we need to explain what is javascript runtime and why we should care about its speed.
在深入讨论之前,我们需要解释什么是JavaScript运行时,以及为什么我们应该关心其运行速度。
Imagine you’ve written a story in JavaScript and you need someone to read it out loud. A JavaScript runtime is like that friendly narrator who brings your story to life! It’s the special environment where your JavaScript story gets read and acted out. But let’s dive a bit deeper. Technically, this ‘narrator’ consists of components like a JavaScript Engine, which is the heart of the runtime, doing the job of understanding and running your code. It’s paired with tools like an Event Loop, which manages tasks and ensures your code doesn’t stumble on its own feet, and a Memory Heap, where all the characters (or variables) in your story get their own space. There’s also a Call Stack, which keeps track of where the story’s action is happening, scene by scene.
想象一下,你用JavaScript写了一个故事,需要有人大声朗读出来。JavaScript运行时就像那个友好的讲述者,把你的故事带入生活!这是你的JavaScript故事被阅读和演绎的特殊环境。但让我们深入一点。从技术上讲,这个“讲述者”由诸如JavaScript引擎这样的组件组成,这是运行时的核心,负责理解和运行你的代码。它配备了像事件循环这样的工具,管理任务并确保你的代码不会自己绊倒,以及一个内存堆,所有的角色(或变量)在你的故事中都有自己的空间。还有一个调用栈,它跟踪故事的动作发生在哪里,一幕一幕地进行。

Intro to Bun 1.0 Bun 1.0 简介

Bun is a new JavaScript runtime which has several major advantages over the popular Node JS and Deno. It was designed to make the apps faster without the need of adding additional complexity to the code.
Bun是一个新的JavaScript运行时环境,它相较于流行的Node JS和Deno具有几个主要优势。它的设计目标是在不增加代码复杂性的情况下,使应用运行得更快。
It is designed to be a drop-in replacement for Node.js. So when using Bun, you don’t need node, nodemon — it has built-in watch mode, dotenv, cross-env — it reads .env files by default.
它被设计为Node.js的替代品。因此,当你使用Bun时,你不需要node, nodemon —— 它具有内置的观察模式, dotenvcross-env —— 它默认读取 .env 文件。
Bun also can run different files such as .js, .ts, .mjs, .jsx, .cjs and .tsx, which means now you don’t need babel, tsc, ts-node and tsx to be installed into your project.
Bun还可以运行不同的文件,如 .js.ts.mjs.jsx.cjs.tsx ,这意味着现在您不需要安装babel, tscts-nodetsx 到您的项目中。
It is a JavaScript bundler with incredible performance and esbuild-compatible plugin API, so you don’t need esbuild, webpack and parcel either.
这是一个具有惊人性能和插件API的JavaScript打包工具,所以你不需要 esbuildwebpackparcel
And one of the most significant benefit over npm and yarn is its speed. Bun is npm-compatible package manager with all familiar commands that yarna and npm have. It also reads the package.json file and writes to node_modules, but 30x faster, which is a game changer.
而与 npmyarn 相比,它最显著的优点之一就是速度。Bun是一个与yarna和npm具有相同命令的npm兼容的包管理器。它还能读取 package.json 文件并写入 node_modules ,但速度快了30倍,这是一个改变游戏规则的特点。
It has built-in Jest-compatible test runner so you can write unit tests without installing additional dependencies.
它内置了与Jest兼容的测试运行器,因此您可以编写单元测试而无需安装其他依赖项。
As it is designed to be drop-in replacement for Node.js it has built-in support for the common Node.js modules like path, fs and net, global variables like __dirname and process.
由于它被设计成可以直接替代Node.js,因此它内置了对常见Node.js模块的支持,如 pathfsnet ,以及全局变量 __dirnameprocess

Benchmark comparison 基准比较

As we said Bun is 4x faster than Node.js, it makes the code lighter which is leading to lower execution time and it is simple to use.
正如我们所说,Bun比Node.js快4倍,它使代码更轻,从而导致更低的执行时间,并且使用起来非常简单。
$ bun install
notion image
The differences running tests is even crazier.
跑步测试的差异更加疯狂。
$ bun test
notion image
It is simple to use. Here is an example of a HTTP server using Bun.
使用起来很简单。这里是一个使用Bun的HTTP服务器的示例。
const server = Bun.serve({ port: 3000, fetch(request) { return new Response(“Welcome to Bun!”); },});console.log(`Listening on localhost:${server.port}`);

Should you switch to Bun over Node.JS

你是否应该从Node.JS转向Bun
To wrap up, Bun is like a new cool toy in the JavaScript world. If you’re thinking about what to use for your next project, you might want to give Bun a try. It’s reliable, has some neat built-in stuff that Node.js doesn’t have, and it’s pretty fast too.
总结一下,Bun就像是JavaScript世界中的一个新酷玩具。如果你正在考虑下一个项目要用什么,不妨试试Bun。它可靠,内置了一些Node.js没有的很棒的功能,而且速度也相当快。
I’m excited to see what the Bun team will do next. And if you want to learn more about it, check out their official guide here. It has all the details you might need!
我很期待看到Bun团队接下来会做什么。如果你想了解更多信息,请查看他们的官方指南。里面包含了你可能需要的所有细节!