Node.js 正在消亡!Bun 1.0 正在改变 JavaScript 游戏(译文)

【关于TalkX】

TalkX是一款基于GPT实现的IDE智能开发插件,专注于编程领域,是开发者在日常编码中提高编码效率及质量的辅助工具,TalkX常用的功能包括但不限于:解释代码、中英翻译、性能检查、安全检查、样式检查、优化并改进、提高可读性、清理代码、生成测试用例等。

TalkX建立了全球加速网络,不需要考虑网络环境,响应速度快,界面效果和交互体验更流畅。并为用户提供了OpenAI的密钥,不需要ApiKey,不需要自备账号,不需要魔法。

TalkX产品支持:JetBrains (包括 IntelliJ IDEA、PyCharm、WebStorm、Android Studio)、HBuilder、VS Code、Goland.

Before going further, we need to explain what is javascript runtime and why we should care about its speed.

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 故事可以被朗读和表演出来。不过,让我们深入了解一下。从技术上讲,这个 "旁白 "由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.

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 .envfiles by default.

Bun also can run different files such as .js, .ts, .mjs, .jsx, .cjsand .tsx, which means now you don’t need babel, tsc, ts-nodeand tsx to be installed into your project.

It is a JavaScript bundler with incredible performance and esbuild-compatibleplugin API, so you don’t need esbuild, webpackand parceleither.

And one of the most significant benefit over npmand yarnis its speed. Bun is npm-compatible package manager with all familiar commands that yarna and npm have. It also reads the package.jsonfile and writes to node_modules, but 30x faster, which is a game changer.

It has built-in Jest-compatible test runner so you can write unit tests without installing additional dependencies.

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, fsand net, global variables like __dirnameand process.

Bun 是一种新的 JavaScript 运行时,与流行的 Node JSDeno 相比,它有几大优势。它旨在提高应用程序的速度,而无需增加代码的复杂性。

它被设计为 Node.js 的直接替代品。因此,使用 Bun 时,你不需要 node、nodemon(它有内置的监视模式)、dotenvcross-env(它默认读取.env文件)。

Bun 还可以运行不同的文件,如 .js.ts.mjs.jsx.cjs.tsx,这意味着现在你不需要在项目中安装 babel、tscts-nodetsx

它是一个 JavaScript 捆绑程序,具有令人难以置信的性能和兼容 esbuild的插件 API,因此您也不需要 esbuildwebpackparcel

npmyarn相比,Bun 的最大优势之一就是它的速度。Bun 是与 npm 兼容的软件包管理器,拥有 yarna 和 npm 所拥有的所有熟悉命令。它也能读取 package.json文件并写入 node_modules,但速度快了 30 倍,改变了游戏规则。

它内置了兼容 Jest 的测试运行程序,因此你无需安装额外的依赖就能编写单元测试。

由于它被设计为 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 install 

Node.js 正在消亡!Bun 1.0 正在改变 JavaScript 游戏(译文)_第1张图片

The differences running tests is even crazier.

而运行测试的差异则更加疯狂。

$ bun test

Node.js 正在消亡!Bun 1.0 正在改变 JavaScript 游戏(译文)_第2张图片

It is simple to use. Here is an example of a HTTP server using Bun.

它的使用很简单。下面是一个使用BunHTTP服务器的示例。

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

你应该切换到 Bun 而不是 Node.JS

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.

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!

What are your thoughts on Bun? Is it going to replace Node.js? Let’s discuss in the comments below!

总而言之,Bun 就像是JavaScript 世界中的一个新奇玩具。如果你正在考虑下一个项目该用什么,不妨试试Bun**。它很可靠,内置了一些Node.js没有的功能,而且速度也很快。

我很期待Bun团队下一步的工作。如果你想了解更多,请查看他们的官方指南 此处。里面有你可能需要的所有细节!

你对 Bun 有什么看法?它是否会取代 Node.js?让我们在下面的评论中讨论一下吧!*

【参考文献】

文章:《Node.js is Dying! Bun 1.0 is Changing the JavaScript Game》

作者:Svetloslav Novoselski

日期:9月11号

上述译文仅供参考,原文请查看下面链接,解释权归原作者所有

⚠️:文章翻译如有语法不准确或者内容纰漏,欢迎评论区指正。

【关于TalkX】

TalkX是一款基于GPT实现的IDE智能开发插件,专注于编程领域,是开发者在日常编码中提高编码效率及质量的辅助工具,TalkX常用的功能包括但不限于:解释代码、中英翻译、性能检查、安全检查、样式检查、优化并改进、提高可读性、清理代码、生成测试用例等。

TalkX建立了全球加速网络,不需要考虑网络环境,响应速度快,界面效果和交互体验更流畅。并为用户提供了OpenAI的密钥,不需要ApiKey,不需要自备账号,不需要魔法。

TalkX产品支持:JetBrains (包括 IntelliJ IDEA、PyCharm、WebStorm、Android Studio)、HBuilder、VS Code、Goland.

你可能感兴趣的:(实用干货分享,node.js,javascript,游戏)