网站首页 > 技术文章 正文
支持 Windows
2024 年 4 月 1 日 Bun v1.1 如期发布,开发者现在可以在 Windows 10 及更高版本上运行 Bun。 这是一个巨大的里程碑,也意味着 Bun 能让更多的开发者受益。
Windows 上的 Bun 通过了 macOS 和 Linux 上的 Bun 测试套件的 98% 测试。 这意味着从运行时、测试运行器、包管理器到打包器的所有内容都可以在 Windows 上运行。
要开始在 Windows 上使用 Bun,可以在终端中运行以下命令:
powershell -c "irm bun.sh/install.ps1 | iex"
Windows 上运行 Bun 带来了巨大的性能提升,比如:
- 安装 Vite React 应用程序时,bun install 的运行速度比 Windows 上的 Yarn 快 18 倍,比 npm 快 30 倍
- 支持使用 Bun run 来运行脚本,这是比 npm run 更快的替代方案,最终比 npm run 快 11 倍,bunx 也比 npx 快 11 倍。
- 支持 bun --watch,优化了大量 control-s 和进程重新加载之间所需的时间。
Windows 上的 Node.js API
Bun 在 Windows 上优化了 Node.js API 以使用 Windows 上最快的系统调用。
例如,Bun 上的 fs.readdir() 比 Windows 上的 Node.js 快 58%。
fs.readdir(__dirname, (err, files) => {
if (err)
console.log(err);
else {
console.log("\nCurrent directory filenames:");
files.forEach(file => {
console.log(file);
})
}
})
大型项目启动速度提高 2 倍
Bun 内置了对 JavaScript、TypeScript 和 JSX 的支持,由 Bun 高度优化的本机代码编写的转译器提供支持。
Summary
bun --bun ./node_modules/.bin/tsc --help ran
1.38 ± 0.06 times faster than node ./node_modules/.bin/tsc --help
2.40 ± 0.09 times faster than bun-1.0.14 --bun ./node_modules/.bin/tsc --help
自 Bun 1.0 以来为大于 50KB 的文件实现了内容可寻址缓存,以避免重复转译相同文件的性能开销。从而使得 tsc 等命令行工具的运行速度比 Bun 1.0 快 2 倍。然而 Bun v1.1 性能得到明显提升。
支持 Bun Shell
Bun 现在是一个跨平台的 shell,类似于 bash,但也可以在 Windows 上运行。
import {spawnSync} from "child_process";
// 这里可以做很多事情
const {status, stdout, stderr} = spawnSync("ls", ["-l", "*.js"], {
encoding: "utf8",
});
Bun Shell 是一个词法分析器、解析器和解释器,实现了类似 bash 的编程语言,以及一系列核心实用程序,例如 ls、rm 和 cat。 shell 还可以使用 Bun.$ API 从 JavaScript 和 TypeScript 运行。
import {$} from "bun";
// 输出到 stdout
await 最快 JavaScript 运行时 Bun 宣布支持 Windows!-今日头条 ls *.js`;
// 输出到 string 字符串
const text = await 最快 JavaScript 运行时 Bun 宣布支持 Windows!-今日头条 ls *.js`.text();该语法使得在 shell 和 JavaScript 之间传递参数、缓冲区和管道变得容易:
const response = await fetch("https://example.com/");
// 响应输出到 stdin
// 将 stdout 写入到 JavaScript
const stdout = await 最快 JavaScript 运行时 Bun 宣布支持 Windows!-今日头条 gzip -c <${response}`.arrayBuffer();Bun.Glob
Bun 现在有一个内置的 Glob API,用于使用 glob 模式匹配文件和字符串,其与流行的 Node.js 库(如 fast-glob 和 micromatch)类似,但它匹配字符串的速度快了 3 倍。
使用 glob.match() 将字符串与 glob 模式进行匹配:
import {Glob} from "bun";
const glob = new Glob("**/*.ts");
const match = glob.match("src/index.ts");
// 输出 true
使用 glob.scan() 通过 AsyncIterator 列出与 glob 模式匹配的文件:
const glob = new Glob("**/*.ts");
for await (const path of glob.scan("src")) {
console.log(path);
// 输出内容 "src/index.ts", "src/utils.ts"
}
更多关于 Bun v1.1 新特性可以参考文末资料,本文不再过多展开。
参考资料
https://bun.sh/blog/bun-v1.1#large-projects-start-2x-faster
https://www.geeksforgeeks.org/node-js-fs-readdir-method/
https://www.oschina.net/news/285850/bun-1-1-released
猜你喜欢
- 2024-10-13 Node.js结合uni-app对微信公众号网页开发中的JS-SDK权限验证配置
- 2024-10-13 设置Node.js脚本开机自启动(node.js 开机启动)
- 2024-10-13 干货 | Node.js在携程的落地和最佳实践
- 2024-10-13 美国云服务器支持Node.js运行的简单方法
- 2024-10-13 Node.js是什么?怎样快速入门?(node.js百度百科)
- 2024-10-13 2022年,开发独立 EXE 桌面应用程序,用什么语言、技术合适
- 2024-10-13 7个免费、开源且实用的Node包,你还在等什么?
- 2024-10-13 极简Node.js安装(node.js下载安装教程)
- 2024-10-13 Node.js 的安装(node js 安装)
- 2024-10-13 安装ES7.6.1(安装空调)
- 最近发表
-
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)