Why another JavaScript engine

QuickJS is great, and I tried hard to just use it. This page is about the four jobs where it kept getting in the way, and the engine we built for them instead.

See the four jobsCompare engines

QuickJS is hard to beat

Writing a JavaScript interpreter is easy; writing one that is faster than QuickJS is not.

QuickJS, especially as quickjs-ng and quickjs-wasi, is small, complete and quick to start. Point a coding agent at building an interpreter with naive instructions and you find out fast: QuickJS is really hard to beat on speed.

So tinyscript is measured against QuickJS on every change. Callgrind counts the instructions both engines retire on the same programs, with the same output required.

  • tinyscript
  • QuickJS
  • QuickJS-ng
  • Clamp a retry deadline with Math.min/max66.7% fewer than QuickJS
  • Numeric loop with a helper53.3% fewer than QuickJS
  • Branching reconnect policy32.5% fewer than QuickJS
  • Rolling 32-bit checksum with Math.imul23.9% fewer than QuickJS
  • Fold delivered byte counts across telemetry batches0.1% more than QuickJS
Instructions retired on five of the 18 workloads, counted by Callgrind on an AMD EPYC 9V74: shorter is better. All 18, with how to rerun them, are in the published results.

Four jobs QuickJS was not built for

Some requirements can't be bolted onto an engine afterwards; they decide its design.

  • Many sessions in one processHundreds of thousands of JavaScript sessions, with nothing spent on the ones that aren't running.
  • Full control over the outside worldA deterministic runtime, whose clock, randomness and messages all come from your application.
  • Untrusted codeA sandbox for code your users or a model wrote.
  • The same code, many timesWork done ahead of time pays for itself when a program runs on every event or request.

Our actor runtime needed all four at once. So does an agent running code mode, and so does a platform that runs its customers' plugins on every request.

Each alternative gives one up

On a server, every usual way to run untrusted JavaScript trades away one of the four.

OptionWhat it costs
QuickJS in WebAssemblyA second engine around the first, to make it a sandbox.
V8 isolatesToo heavy for one per session, and not a security boundary alone.
Ahead-of-time compilers such as scriptcNative programs, not code you can safely run in your process.

tinyscript aims at all four jobs, inside your process. The comparison also covers djs and Psychoscript, which aim at code mode too.

Safe by construction

A sandbox that filters a script's files and sockets is only as safe as its filter.

So tinyscript has nothing to filter. All it asks of your program is a buffer of bytes, and it does all of its work in there.

When a script needs the outside world, it writes a message into another buffer and your program writes the answer back. Because everything is deterministic, you can save a session, restore it later, or inspect everything it holds.

Idle sessions cost no time

An engine that runs each script to completion makes every session wait for the slowest one, and an endless loop waits forever.

tinyscript runs a call for the fuel you give it, then returns to your program. You continue the call exactly where it stopped, or abandon it, so a session that isn't running takes no time at all.

One compiled program runs in many realms, each with its own globals and heap. Between calls, a realm's stacks hold nothing live.

Fuel bounds every call

ta_tinyscript_run runs a call for at most the fuel it is given, and the next run continues where it stopped.

Source:tinyscript, include/tinyscript.h

Fast by design

Parsing the same plugin on every request spends most of the request before the plugin runs.

tinyscript compiles to bytecode once and runs that bytecode through its VM. TypeScript annotations are hints for that compiler; it doesn't check types and never will, because tsc does that well.

QuickJS was born in a world without WebAssembly. Now WebAssembly is everywhere, even where you don't see it, so we made it tinyscript's bytecode.

That paves the way for whole-program compilation: JavaScript or TypeScript in, one WebAssembly module of your whole program out. Node.js, Bun, a browser or any WebAssembly runtime then compiles it to native code.

An image is standard WebAssembly

A pure numeric module instantiates with new WebAssembly.Instance(module) in a standard engine.

Source:tinyscript, README.md

How far it has come

Depending on an engine means knowing exactly what runs today.

tinyscript is an early alpha: APIs will change, and its internals are not a contract. It runs every source as strict code, so with is always an error.

Here is the honest number: the share of test262, the ECMAScript conformance suite, that passes today. It goes up with almost every commit.

All of test262, in strict mode62.9%31,310 of 49,767
Language syntax and semantics85.2%18,969 of 22,259
Built-in objects51.2%12,130 of 23,692
Annex B, web compatibility30.4%89 of 293
Internationalization (ECMA-402)0.6%22 of 3,406
test262 in strict mode at tinyscript's pinned commit, by its own run.

Read the referenceMeasure it yourself