A new generation of javaScript runtime that beats Node.js and Deno with performance - Bun.js

Today I would like to introduce to you a latest open source  javaScript runtime: Bun.js.

image.png

image.png

Just got a  26.1k star in less than a month! It looks like it's going to be   a big competitor to and Node.js ! Deno

Node.js Unlike the traditional  runtime  javaScript , Bun.js the packager, transpiler, task runner and  npm client are directly built in, which means that you  Webpack/Rollup/esbuild/Snowpack/Parcel/Rome/swc/babel can run it directly  without any need TypeScript、JSX!

In addition, Bun.js hundreds of sums are natively supported  Node.js ,  Web APIincluding functions of approx  90% (  Node-API etc. fs、path、Buffer ).

Bun.js The goal is to run most of the world outside of the browser,  JavaScriptbring performance and complexity enhancements to your future infrastructure, and increase developer productivity with better, simpler tools!

How is the performance?

Server-side rendering:  HTTP requests processed per second

image.png

Loading a huge  sqlite table: Average queries per second

image.png FFI: operations per second

image.png

Why so fast?

And  Node.js、Deno different, Bun.js it is not based on  V8 the engine, it directly selects the  JavaScriptCore engine, and its execution speed tends  V8 to be faster than other more traditional engines.

Also, Bun.js being written in a low-level programming language with manual memory management, low  ZIG -level control over memory, no hidden control flow, is probably the secret to its very good performance.

Bun.js 的大部分内容都是完全从零开始编写的,包括 JSX/TypeScript 转译器、npm 客户端、打包器、SQLite 客户端、HTTP 客户端、WebSocket 客户端等等。

有哪些能力?

  • Web API:对 fetch、WebSocket、 ReadableStream 等 API 都提供了内置支持
  • Node.js 模块:Bun 实现了 Node.js 的模块解析算法,同时支持 ESM 和 CommonJS,但 Bun 内部使用 ESM
  • 支持转译大量文件类型,你可以直接运行 TypeScript、JSX,甚至支持各种 tsconfig.json 中的配置。
Input Loader Output
.js JSX + JavaScript .js
.jsx JSX + JavaScript .js
.ts TypeScript + JavaScript .js
.tsx TypeScript + JSX + JavaScript .js
.mjs JavaScript .js
.cjs JavaScript .js
.mts TypeScript .js
.cts TypeScript .js
.toml TOML .js
.css CSS .css
.env Env N/A
.* file string
  • Bun.write 使用最快的系统调用,实现写入、复制、管道、发送和克隆文件。
  • 自动加载环境变量 .env 文件,不需要再 require("dotenv").load()
  • 附带一个内置的快速 SQLite3 客户端 bun:sqlite
  • Bun.js 实现了大部分 Node-API (N-API),大部分 Node.js 原生模块都可以正常工作。
  • bun:ffi 可以使用低成本的外部函数接口从 JavaScript 调用本机代码(据测试比 napi 快 5 倍 、比 Deno 快 100 倍)

image.png

  • Native support for an ever-growing  Node.js list of core modules as well as global variables such as  Buffer and process

try it

Install  Bun CLI:

curl https://bun.sh/install | bash

Bun The  HTTP server is  Request built   on standards Response such as  :Web

// http.js
export default {
  port: 3000,
  fetch(request) {
    return new Response("Hi, ConardLi!Welcome to Bun!");
  },
};

bun Run it with  :

bun run http.js

Then open it in your browser  http://localhost:3000.

CLI Commands: :  Scripts  in   and   files  and files bun runcan be run directly  .JavaScriptTypeScriptpackage.jsonscripts

According to tests, bun running  the script is  30 times faster package.json than  npm running  the script.package.json

CLI command: bun install: Compatible  npm package manager, use the fastest system call to copy files.

image.png

According to tests, bun installs packages 20 times faster than npm.

CLI Commands: bun wiptest: A similar  Jest test runner for built-  bun in  JavaScript and  TypeScript projects.

Such bunjs is expected to become a member of the replacement node.

Guess you like

Origin juejin.im/post/7121179240032108581