{"author":{"id":"4e3fvwoonbfzbvqtdwehr2fzdp","name":"陈成","avatar_url":"https://lh3.googleusercontent.com/a/ACg8ocLP6uaUWVTzl3dUQgZAldOJdTbU_NCUcOxb0Jx1K5CuD97UlDiQ=s96-c","handle":"sorrycc"},"mine":false,"report":{"topic_id":"sp2xkywxbpx2","markdown":"# moejs 调研\n\n\u003e 2026-11-09 整理。来源：X 书签（3 条）及回复、X 搜索 \"moejs\"（Latest）、GitHub 仓库 README 和 Releases、Reddit 搜索。\n\u003e 2026-11-09 更新：v0.1.0 当天发布，alpha 阶段结束，补充 release 数据。\n\n## 一句话\n\nmoejs 是 [@Ion_Mio_](https://x.com/Ion_Mio_)（离离子，new-api 作者）写的**纯 Go JavaScript 运行时**，目标很窄：在 Go 进程里**快速、隔离地**跑大量小 JS 插件。没有 cgo，也没有汇编。当前版本是 `v0.1.0`（11-09 发布，alpha 阶段结束），GitHub 上约 510 star，仓库已从 `Calcium-Ion/moejs` 迁到 [`MoeForge/moejs`](https://github.com/MoeForge/moejs)。\n\n## 它解决什么问题\n\nGo 程序想支持用户插件，以往有几条路：\n\n| 方案 | 问题 |\n|---|---|\n| goja / Sobek（纯 Go） | 能用，但慢、每个 runtime 内存大，现代 ES 特性不全 |\n| quickjs-go / v8go（cgo） | 要 C 工具链，交叉编译麻烦，新建 runtime 很贵（V8 约 1 ms） |\n| modernc QuickJS（C 转 Go）、QJS（QuickJS + wazero） | 纯 Go，但单次调用和新建 runtime 成本高 |\n| 外挂 Node 进程 | 进程间通信开销，隔离和部署更复杂 |\n\nmoejs 想做到**每个并发请求都配一个独立 runtime**也不心疼。\n\n## 性能（仓库 README 数据）\n\nnew-api 的 10 个任务插件、269 次真实调用，在 Go 调用方一侧计时（2026-10-01）：\n\n| 指标 | moejs | Sobek | QuickJS 纯 Go (modernc) | QuickJS (cgo) | V8 (v8go) |\n|---|---|---|---|---|---|\n| 单次插件调用 | **6.9 µs** | 14.3 µs | 35.2 µs | 104.5 µs | 56.6 µs |\n| 新建 runtime | **1.4 µs** | 2.2 µs | 180 µs | 382 µs | 1,153 µs |\n| 每 runtime 内存 | **81 KiB** | 264 KiB | 227 KiB | 348 KiB | 1,544 KiB |\n\n- 对比 Sobek：调用耗时约一半，内存约 1/3。\n- V8 benchmark suite v7 得分 1,429，是 C QuickJS 的 1.35 倍，8 项全部最高。\n- C 直调的 QuickJS 处理小请求仍比 moejs 快约 7%。\n- test262：80,971 通过、0 失败，另有 14,268 个用到未实现特性的被跳过。\n\n### v0.1.0 的新数据（release notes）\n\n- V8 benchmark suite v7：8 个程序固定跑一遍耗时 14.29 s，alpha.11 要 41.55 s，快近 3 倍。\n- new-api 插件负载，8 个 worker，用 `ParseJSONString`：10 万 token 的 agent 请求吞吐是 C QuickJS 的 2.9 倍，8 MiB 图片请求是 8.6 倍，所有场景都领先 Sobek、modernc 和 quickjs-go。\n- 内存：64 位上对象头从 96 字节降到 48 字节，闭包从 192 降到 160/144，绑定函数从 208 降到 176。\n- 正则：Go regexp 能表达的改走原生匹配器，O(n·m)；先找每个匹配必含的字面量，找不到直接失败。\n- Go 传入的字符串和 JSON 字符串保留 UTF-8，JS 第一次读取时才解码。\n- `MaxCallDepth` 从 512 提到 4096（React 服务端渲染深树需要）；`MemoryLimit` 计入寄存器栈和帧表。\n- node / webapi 在 `CGO_ENABLED=0` 下能编译到所有 Go 目标，包括 32 位、js/wasm、wasip1、plan9；node:fs 支持 Windows。\n- eventloop 新增 `OnIdle` / `OnWake`，宿主可以在循环等待时暂停 CPU 看门狗或让出执行槽位。\n- 新增 `bench/cmd/pluginbench`、`bench/cmd/v8bench`，可以复现插件和 V8 suite 的数据。\n- 根包、node、webapi 相对 alpha.11 没有 API 变化；engine 和 bytecode 包有破坏性改动。\n\n\u003e 注意：这些都是作者自测，场景偏向 new-api 插件负载。评论区有人问\"和 goja 比如何\"、\"alpha.9 的提升是冷启动还是长对话\"，作者没有展开回答。\n\n## 架构与能力\n\n```mermaid\nflowchart LR\n  subgraph Host[\"Go 宿主进程\"]\n    Pool[\"Runtime 池\u003cbr/\u003e(chan *Runtime)\"]\n    HF[\"宿主函数\u003cbr/\u003e(普通 Go func，可返回 Promise)\"]\n    Imp[\"Importer\u003cbr/\u003e(所有 import 都经过它)\"]\n  end\n  subgraph RT[\"moejs Runtime（每请求一个）\"]\n    Builtins[\"冻结共享的内建对象\u003cbr/\u003e(SES lockdown 模型)\"]\n    Globals[\"独立 globals / 时区\"]\n    Plugin[\"插件代码 JS / TS\"]\n  end\n  Pool --\u003e RT\n  HF -. 注入 .-\u003e Globals\n  Imp -. 控制 .-\u003e Plugin\n  Plugin --\u003e|结果| Dec[\"Go struct / JSON\u003cbr/\u003e(DecodeOptions 限大小)\"]\n  subgraph Opt[\"可选包\"]\n    EL[\"eventloop\u003cbr/\u003esetTimeout 等\"]\n    WA[\"webapi\u003cbr/\u003efetch / Streams / URL\"]\n    ND[\"node\u003cbr/\u003enode:fs / buffer\"]\n  end\n  Opt -.-\u003e RT\n```\n\n要点：\n\n- **默认无能力**：裸 runtime 没有定时器、网络、文件系统。插件只能看到标准库和宿主装进去的东西。作者原话：\"moejs 不给程序任何宿主能力，所以正常情况下会更严格。\"\n- **传值省拷贝**：`ParseJSONString` 直接读调用方字符串；map 按插件实际读取的层级惰性转换；结果可直接解到 Go struct。\n- **可中断**：任意 goroutine 能打断死循环；JS throw、语法错误、中断、宿主 panic 各有对应的 Go 错误类型，栈格式与 V8 一致。\n- **内存上限**：`Options.MemoryLimit` 限制单次请求分配量，脚本无法 catch。\n- **TypeScript**：`CompileTS` 按 Node 的方式擦除类型，报错位置指向 TS 源码；enum、带值 namespace 不支持。\n- **webapi.fetch**：宿主通过 `http.Client` 和 allow hook 决定能访问哪里，响应流式读取，字节计入内存上限。\n- **node:fs**：路径锁在一个宿主目录内（`os.Root`，Linux 上用 `openat2 RESOLVE_BENEATH`），默认只读。\n- 附带 `default.pgo`，构建时需手动 `-pgo=` 指定。\n- 唯一依赖是 [moego](https://github.com/MoeForge/moego)：base64 在 amd64 上走 AVX2、arm64 上走 NEON，其他架构或 `-tags purego` 时是纯 Go。moejs 自己的代码仍没有汇编和 cgo。\n\n**未实现**：import attributes / JSON modules、`using`、decorators、iterator helpers、Temporal、ShadowRealm、FinalizationRegistry，以及部分 Intl。\n\n**设计借鉴**（README 致谢）：goja/Sobek（Go 互操作约定）、QuickJS（16 字节值布局、atoms、shape）、V8（hidden class、inline cache、Ignition 寄存器解释器）、Lua 5（定长寄存器指令）、JSC/SpiderMonkey（NaN-boxing）、esbuild（Go 写快速 parser）、SES（lockdown）。\n\n## 版本时间线\n\n```mermaid\ntimeline\n  title moejs 公开进展（2026）\n  10-06 : yetone 宣布 magpie 网关中间件跑在 moejs 上（约 1 µs/流式事件）\n  10-07 : lyricwai 用 moejs 实现 Pi 风格 codemode（Mister Morph）\n  10-08 : 一批性能优化，加入对比基准\n  10-14 : alpha.7，支持直接写 TypeScript，按请求设内存上限\n  10-21 : alpha.9，性能再提升（27 万浏览）\n  10-23 : dsh-ptc-moejs，给 DeepSeek harness 做 PTC 运行时\n  10-26 : 迁入 MoeForge 组织\n  10-28 : 姊妹项目 moego（Go 优化包，Base64 SIMD）\n  alpha.10 : Intl（CLDR 48，11 种语言），webapi fetch/Streams，MemoryLimit 计存活堆\n  alpha.11 : 模块路径改为 MoeForge，node:fs/buffer，await 零分配\n  11-06 : egoist 的 MyGo Music 插件系统用 moejs\n  11-07 : Metrix.Bar（MyGO + MoeJS 驱动的 menubar 应用）公测\n  11-09 : v0.1.0 发布，alpha 结束；V8 suite 比 alpha.11 快近 3 倍\n```\n\n## 谁在用\n\n| 项目 | 用法 | 来源 |\n|---|---|---|\n| **new-api** | 插件宿主，`pkg/jsplugin` 决定 moejs 要支持哪些 API；也是基准负载来源 | [README](https://github.com/MoeForge/moejs) |\n| **magpie**（yetone） | 网关中间件进程内运行，处理每个 Agent 的请求和回复，约 1 µs/事件 | [@yetone](https://x.com/yetone/status/2107172155796636119) |\n| **MyGo Music**（egoist） | 音乐应用的插件运行时；社区已有 `hubandad/mygo-music-netease` 等插件 | [@localhost_4173](https://x.com/localhost_4173/status/2118729555209031912)、[@sisuer_cn](https://x.com/sisuer_cn/status/2118737137063571952) |\n| **mygo-plugin** | MyGo 应用的原生插件宿主，每个插件一个 moejs runtime，带权限、存储、超时、CLI | [Awesome MyGo](https://awesomemygo.dev/plugins) |\n| **Metrix.Bar**（面条） | menubar 应用，\"JS 写逻辑，Go 渲染 UI\"，Agent 可动态生成看板 | [@miantiao_me](https://x.com/miantiao_me/status/2118889977241325625) |\n| **Mister Morph**（lyricwai） | Agent CLI 的 codemode | [@lyricwai](https://x.com/lyricwai/status/2107747936516141449) |\n| **dsh-ptc-moejs** | DeepSeek harness 的 PTC 运行时，作者称比默认 Node PTC 快 | [@Ion_Mio_](https://x.com/Ion_Mio_/status/2113323571997282559) |\n\n可以看出，moejs 目前已在中文 AI / 独立开发圈里形成一个小生态：**new-api / magpie（AI 网关） + MyGO（桌面 UI） + moejs（动态逻辑）**。\n\n## 社区讨论要点\n\n**正面**\n\n- \"竟然比 quickjs 还要快\"（@ImHanxi）\n- \"准备换掉 goja\"（@pK7qkLEE6TKEBRs）\n- \"MyGO 和 MoeJS 是绝配，后面写桌面应用，我基本上都会用这两个来搞\"（@miantiao_me，1.8 万浏览）。被问到怎么用时，他说\"需要动态性的地方用 moejs 来做\"；被问\"AI 会写吗\"，答\"会\"。\n- @t0ng7u：\"Go 里嵌 JS 跑插件可能是最快的引擎\"\n\n**疑问 / 风险**\n\n- 和 goja 的直接对比：作者称\"晚点跑一个\"，并让人参考 Sobek（goja 的 fork）的数据。v0.1.0 加了可复现的 bench 命令，但仍没有 goja 本身的数字。\n- 长时间运行的内存稳定性：作者答\"反正不烧心\"，没有给数据。\n- dsh-ptc-moejs 图上快两个数量级，有人质疑隔离强度是否相同。作者解释 moejs 默认不暴露宿主能力，所以更严格。\n- Windows 桌面端有 bug 反馈（@T_t__biu）。\n- 刚出 alpha，版本号仍是 0.x。alpha.11 改过一次 import 路径；v0.1.0 的根包 API 没变，但 engine / bytecode 包有破坏性改动。\n- @hd_nvim 指出 magpie 的 go.mod 依赖 moejs，作者也在 new-api 团队，\"和这位推主颇有渊源\"。生态里的互相背书要放在这个背景下看。\n\n## Reddit\n\nReddit 上**没有搜到 moejs 的直接讨论**（\"moejs\" 只匹配到无关结果，r/golang 内搜索为空），说明它的传播目前基本局限在中文 X 圈。\n\n可作对照的 r/golang 相关帖：\n\n- [QJS: Run JavaScript in Go without CGO using QuickJS and Wazero](https://www.reddit.com/r/golang/comments/1nw0djm/qjs_run_javascript_in_go_without_cgo_using/)：同样为了替代 goja 做插件系统，走 QuickJS→WASM→wazero 路线。评论里 ncruces 指出，基准中的\"内存\"其实是 Go 分配量，WASM 内部堆对 Go 不可见；基于 context 的中断会在每个回跳处插检查，比较慢。看 moejs 的数据时也值得带着这两个问题。\n- [Getting insight into embedded JavaScript runtimes in Go](https://www.reddit.com/r/golang/comments/1ppy20m/getting_insight_into_embedded_javascript_runtimes/)\n- modernc.org/quickjs - Preliminary performance results（r/golang，3 年前）\n\n## 选型判断\n\n```mermaid\nflowchart TD\n  A[Go 程序要跑 JS 插件?] --\u003e B{需要完整 Node 生态\u003cbr/\u003enpm 包/原生模块?}\n  B -- 是 --\u003e N[外挂 Node / Bun 进程]\n  B -- 否 --\u003e C{能接受 cgo?}\n  C -- 能且要极致单线程性能 --\u003e V[v8go / quickjs-go]\n  C -- 不能/要交叉编译 --\u003e D{高并发、每请求独立沙盒、\u003cbr/\u003eµs 级开销?}\n  D -- 是 --\u003e M[moejs ✅\u003cbr/\u003e刚到 v0.1.0，留意 API 变动]\n  D -- 否, 求稳 --\u003e G[goja / Sobek\u003cbr/\u003e成熟、用户多]\n```\n\n适合用 moejs 的场景：AI 网关中间件、Agent 的 code mode / PTC、桌面应用插件、规则引擎这类**小脚本、高频、要隔离**的负载。不太适合：依赖大量 npm 包，或者需要 decorators、Temporal 等未实现特性。\n\n## 参考链接\n\n**仓库与文档**\n- https://github.com/MoeForge/moejs\n- https://github.com/MoeForge/moejs/releases\n- https://github.com/MoeForge/moejs/releases/tag/v0.1.0\n- https://github.com/MoeForge/moejs/blob/main/docs/performance.md\n- https://github.com/MoeForge/moejs/blob/main/docs/guide.md\n- https://github.com/MoeForge/moego\n- https://github.com/Calcium-Ion/dsh-ptc-moejs\n- https://awesomemygo.dev/plugins\n\n**X 书签**\n- https://x.com/Ion_Mio_/status/2112805808610402340 （alpha.9 发布，27.2 万浏览）\n- https://x.com/yetone/status/2107172155796636119 （magpie 中间件）\n- https://x.com/localhost_4173/status/2118729555209031912 （MyGo Music 插件）\n\n**X 搜索**\n- https://x.com/Ion_Mio_/status/2119684071299436891 （v0.1.0 发布）\n- https://x.com/Ion_Mio_/status/2115421440820560253 （alpha.11）\n- https://x.com/Ion_Mio_/status/2115301646427385882 、https://x.com/t0ng7u/status/2115303168720752951\n- https://x.com/Ion_Mio_/status/2114404698300285391 （迁入 MoeForge）\n- https://x.com/Ion_Mio_/status/2113323571997282559 （dsh-ptc-moejs）\n- https://x.com/Ion_Mio_/status/2110338043307868211 （alpha.7）\n- https://x.com/Ion_Mio_/status/2108133257942601880 （性能更新）\n- https://x.com/lyricwai/status/2107747936516141449 （codemode）\n- https://x.com/hd_nvim/status/2109396235023249739\n- https://x.com/visamzh/status/2112818469511279053\n- https://x.com/fondoger/status/2113824065224065112\n- https://x.com/miantiao_me/status/2118889977241325625 （Metrix.Bar）\n- https://x.com/miantiao_me/status/2119349090526081253 （MyGO + MoeJS）\n- https://x.com/suzukaze_haduki/status/2118934348095254833 （mygo-plugin）\n- https://x.com/sisuer_cn/status/2118737137063571952\n\n**Reddit**\n- https://www.reddit.com/search/?q=moejs （无相关结果）\n- https://www.reddit.com/r/golang/comments/1nw0djm/qjs_run_javascript_in_go_without_cgo_using/\n- https://www.reddit.com/r/golang/comments/1ppy20m/getting_insight_into_embedded_javascript_runtimes/\n","html":"","default_view":"markdown","version":2,"updated_by":"4e3fvwoonbfzbvqtdwehr2fzdp","updated_at":"2026-11-09T07:59:38Z","assets":[],"base_url":"/pub/sp2xkywxbpx2/p/"},"sources":[],"topic":{"id":"sp2xkywxbpx2","title":"moejs","question":"moejs 是什么、性能和隔离怎么样、值不值得在 Go 里用来跑 JS 插件？","visibility":"public","slug":"moejs","hue":49,"emoji":"⚡","owner_id":"4e3fvwoonbfzbvqtdwehr2fzdp","created_at":"2026-11-09T07:59:28Z","updated_at":"2026-11-09T07:59:39Z","published_at":"2026-11-09T07:59:39Z","item_count":0,"report_words":1547,"formats":["markdown"],"url":"https://research.sorrycc.dev/@sorrycc/moejs"}}
