Today I would like to introduce to you a latest open source javaScript
runtime: Bun.js
.
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 API
including 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, JavaScript
bring 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
Loading a huge sqlite
table: Average queries per second
FFI
: operations per second
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
倍)
- Native support for an ever-growing
Node.js
list of core modules as well as global variables such asBuffer
andprocess
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
.
- See more examples: github.com/Jarred-Sumn…
- Check out the official documentation: github.com/Jarred-Sumn…
CLI Commands: : Scripts in and files and files bun run
can be run directly .JavaScript
TypeScript
package.json
scripts
According to tests,
bun
running the script is 30 times fasterpackage.json
thannpm
running the script.package.json
CLI command: bun install
: Compatible npm
package manager, use the fastest system call to copy files.
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.