返回资讯中心
外部精选
软件工程
#开发者实时流

Bun PR – Compile TypeScript to machine code ahead of time

Compile TypeScript to machine code ahead of time: bun build --compile --aot - #45631

Hacker News 最新tempaccount4202026/10/17 · 2026/10/1730 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

Code

Issues3.5k (3.5k)

Pull requests5k+ (5k+)

Discussions

Actions

Security and quality

Insights

More items

Compile TypeScript to machine code ahead of time: bun build --compile --aot - #45631

#45631

Draft

Jarred-Sumner wants to merge 135 commits intomainoven-sh/bun:mainfrom claude/compile-aot-on-checkoven-sh/bun:claude/compile-aot-on-checkCopy head branch name to clipboard

ConversationCommits135 (135)ChecksFiles changed

Draft

Compile TypeScript to machine code ahead of time: bun build --compile --aot#45631

Jarred-Sumner wants to merge 135 commits intomainoven-sh/bun:mainfrom claude/compile-aot-on-checkoven-sh/bun:claude/compile-aot-on-checkCopy head branch name to clipboard

Conversation

Jarred-Sumner

commented

Oct 17, 2026

•

edited

Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link

Copy Markdown

Collaborator

bun build --compile --aot compiles TypeScript and JavaScript to machine code when you build. The executable has no JIT and no bytecode, and it never asks the OS for executable memory.

The TypeScript 6 compiler, built from its own TypeScript source, type checking the effect repo:

Same compiler, same source. 3.7x faster than running it on a JIT, with 5.6x less CPU time and 2.9x less memory. One thread against one thread, it's faster than the Go port. By default the Go port type checks in parallel, and that still wins on the clock. Multi-threaded --aot is planned but not implemented yet.

Claude Code's CLI, the released build against the same app built with --aot. A scripted session: start, first prompt, three turns, with the API stubbed out locally.

A third of the CPU time, about a quarter of the heap, and the first prompt is up in less than half the time.

Both on Linux x64 (Zen 4). TypeScript on 8 cores, Claude Code as the mean of 96 sessions.

bun build --compile --aot ./app.ts --outfile myapp

Experimental, and a draft. The engine half is oven-sh/WebKit#757.

JavaScript engines were designed for browsers. Code arrives as text from somewhere you don't trust, has to start running immediately, and can do anything. So the engine interprets it, watches which types show up, guesses, compiles the guess, and throws the compiled code away when the guess was wrong. All of that happens on the machine running the program, every time it starts.

A server or a CLI isn't in that position. You have all the source before you ship it. If it's TypeScript, you've also written down what everything is. Then the build erases that, and every process spends its first seconds working it out again.

class Vec { constructor( public x: number, public y: number, ) {} }

function dot(a: Vec, b: Vec): number { return a.x * b.x + a.y * b.y; }

A Vec is a 16-byte header and two raw doubles. a.x is a load at a fixed offset. No hidden-class check, no inline cache, no boxed number. There is nothing to deoptimize because nothing was guessed.

References between objects are 32 bits. import type { int32 } from "bun" gives you a 4-byte integer field that the build proves and the executable never checks. .js files get compiled too, just without types to work from.

Not a new language, and not a subset. There's no new syntax, nothing to annotate and nothing to port. You point it at the entry point you already have.

The test suite builds and serves Next, Nuxt, Astro, SvelteKit, Angular and React Router apps. It runs Express, Fastify, Koa, Hono, Elysia, NestJS, Apollo, tRPC, socket.io and ws. It runs Drizzle, TypeORM, Sequelize, Mongoose, Knex, Kysely, pg, mysql2, ioredis and the AWS, Stripe, OpenAI, Sentry, Datadog, New Relic and OpenTelemetry SDKs. It runs native addons like sharp, bcrypt and argon2.

About 3,750 smaller programs run both compiled and under regular Bun, and the output has to match.

There are five, and each one is there because the compiler can't generate good code without it.

TypeScript is unsound on purpose. any, as and JSON.parse all let you call something a number when it isn't, and today nothing happens. If a.x is going to be a raw load, that can't stand. Under --aot the types are real:

Those five are the changes to the language. docs/bundler/executables.mdx lists the smaller differences, like the wording of some error messages and what the debugger can do.

function double(n: number): number { return n * 2; }

const body: any = JSON.parse('{"visits":"3"}'); console.log(double(body.visits));

$ bun app.ts 6

$ bun run --aot app.ts 6 | console.log(double(body.visits)); ^ TypeError: expected number | undefined, got string "3" (bun --aot: https://bun.com/docs/bundler/executables#ahead-of-time-compilation) at /path/to/app.ts:6:20

You shouldn't have to find these in production, so there are four steps on the way there:

Since the compiler knows your types, it can also write the validator. Bun.validate<T>(value), Bun.safeValidate<T>(value) and Bun.parse<T>(text) check data against a TypeScript type. There's no schema to keep in sync.

A JIT needs memory it can write machine code into and then run. That is about the most useful thing an attacker can find in a process: turn a memory bug into a write to that region and you're running your own code. JIT compilers are also where many of those memory bugs come from, because a wrong guess about a type, or a bounds check removed by mistake, is a type confusion.

That's why iOS doesn't let third-party apps JIT, why Lockdown Mode turns it off in Safari, and why Edge ships a mode without it. When Microsoft built theirs, they counted roughly 45% of V8's CVEs as JIT bugs.

CI runs executables somewhere that refuses executable memory and checks that they still work:

What runs under that: classes, Maps and promises, an HTTP server and client, regular expressions built at run time, WebAssembly, bun:ffi calls, and eval and new Function, which are interpreted and so need no executable memory either. You can turn those policies on for your own service and leave them on.

Nothing above takes eval or new Function away. They work out of the box, in the interpreter.

But most servers never need them. If yours doesn't, you can switch them off and get a process where the only code that can ever run is the code that was in the file when you shipped it. Each of these is opt-in:

This is possible because the compiled program doesn't depend on the interpreter:

A library that probes with try { new Function("") } catch {} takes its fallback path, which is what you want here anyway. zod 4, ajv, fast-json-stringify and Elysia generate their hot code with new Function at startup, and without a JIT that code stays interpreted. z.config({ jitless: true }) makes parse in zod 4 about 2.5x cheaper under --aot.

A --compile executable is a copy of bun with your program attached. So your hello world ships a package manager, a test runner, a type checker, a linter, a formatter and several JIT compilers.

--compile-trim-runtime leaves them out. It works without --aot too.

You can't delete code from a linked binary, because dead functions are scattered all through .text. So the work happens when bun itself is linked:

After that, bun build --compile just doesn't copy those pages. Each hole becomes one more PT_LOAD. Nothing that stays moves, so there's nothing to relocate, symbols and crash reports still line up, and you don't need a linker or a toolchain. It takes 0.35 s. Each door is overwritten with a stub that prints what was left out and exits with 70.

That removes what no compiled program can reach. The second step removes what your program never names. The compiler already knows which globals, Bun.* APIs and built-in modules you use, so it tells the writer, and the rest stays behind: the transpiler and parsers, the CSS parser, the package manager, sqlite, image codecs, WebAssembly, the inspector, HTMLRewriter, the compression codecs. 25 features so far. That takes another 16 MB off a hello world on Linux x64.

The second step only applies to a program with no eval, new Function or computed require, because a string that becomes code could name anything. Such an executable is built with --disallow-code-generation-from-strings=strict. A program that does use them keeps everything.

There's more to get. Relinking by hand with more functions shut, a Hono server still answered every route at 30 MB, and at 22 MB.

Eight small servers, Bun 1.4.3 → --aot, under load on one core.

Linux x64. Mean of five code layouts, two runs. Requests per second are ±0.4 to ±1.0.

Every one of them uses less memory. RSS is down 17 to 40%, and the bigger frameworks hold about half the private memory. There's no bytecode, no JIT-compiled code and no profiling data to keep around. The code is file-backed pages of the executable, so the kernel shares them between processes and can drop them under pressure.

They also start faster, because there's nothing to parse and nothing to warm up. An Express server goes from 83 MB to 53 MB and answers its first request 24 ms after exec instead of 73 ms. If you pay for containers by the gigabyte, pack many processes on a box or scale to zero, that probably matters more to you than peak throughput on one core.

Peak throughput is where we're behind, and CPU per request follows it. These are mostly untyped npm packages running code that executes once per request. A JIT learns their types by watching. We can't, so every property access goes through a lookup by name. This is the main thing left to fix.

One fix is giving packages their types back. packages/bun-sound holds npm packages with type annotations added and nothing else changed: strip the types and you get npm's bytes back, and a check proves that. With a typed react-reconciler, a React update takes half the instructions.

Those servers are mostly untyped. This is what typed code gets.

test/aot/backend/tracker is an issue tracker in 2,000 lines of strict TypeScript: Bun.serve, Bun.sql on Postgres, no framework, written the way you'd normally write TypeScript. rivals/ has the same app in Go (net/http, pgx) and in Rust (axum, tokio-postgres). Same routes, same SQL, same bytes in every response, checked over 3,300 requests. A mix of its routes, on one core:

Linux x64, three runs, ±2 to 5%. Each at its best database pool size. 1.4.3 can't validate by type, so it runs the same checks written out by hand.

Against Bun 1.4.3 that's 48% more requests per second, a third less CPU per request and 36% less memory. It's 17% behind Go, with about the same private memory. Rust is still twice as fast.

Docs: docs/bundler/executables.mdx has every difference, docs/bundler/aot-adoption.mdx is the migration guide, and docs/bundler/aot-debugging.mdx covers profilers, crash reports and source maps.

There was an error while loading. Please reload this page.

Jarred-Sumner

added 30 commits October 1, 2026 13:09

compile.aot: ahead-of-time compilation for compiled executables

…

bf7a06c

A checkpoint, to be split into a series for review.

bun build --compile --compile-aot compiles the program's JavaScript to machine code when it is built, and puts what the program's code is made of (functions, strings, structures, symbol tables) in a heap that is mapped from the executable. bun run --aot and bun test --aot do the same for one run. ARM64 on macOS and Linux so far: elsewhere the flags are refused.

Also here: sound types (--sound-types), which turn TypeScript annotations into checks that the compiler can rely on; bun-slim, an opt-in target that links the runtime without JavaScriptCore's compilers; crash reports and --cpu-prof for compiled code.

Needs the engine's branch of the same name.

compile.aot: test what is added to built-i

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →