<!-- https://tinyscript.tinyactors.dev/why/ -->
# Why tinyscript

Kind: explanation. For: [Embedder](https://tinyscript.tinyactors.dev/audiences/embedder.xml), [Agent builder](https://tinyscript.tinyactors.dev/audiences/agent-builder.xml), [Platform builder](https://tinyscript.tinyactors.dev/audiences/platform.xml).
Voice: [Dario](https://tinyscript.tinyactors.dev/voices/explanation.xml).

> Why build another JavaScript engine when QuickJS exists: many sessions in one process, determinism, a sandbox for untrusted code, and work done ahead of time.

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 jobs](https://tinyscript.tinyactors.dev/why/index.md#fits) · [Compare engines](https://tinyscript.tinyactors.dev/compare/index.md)

<a id="hard-to-beat"></a>

## 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.

- **Clamp a retry deadline with Math.min/max**: tinyscript, 66.7% fewer than QuickJS
- **Numeric loop with a helper**: tinyscript, 53.3% fewer than QuickJS
- **Branching reconnect policy**: tinyscript, 32.5% fewer than QuickJS
- **Rolling 32-bit checksum with Math.imul**: tinyscript, 23.9% fewer than QuickJS
- **Fold delivered byte counts across telemetry batches**: tinyscript, 0.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](https://github.com/tinyactors-dev/tinyactors/blob/ef90baf70d8974072e9007c482190ec61043ce7c/tinyscript/bench/RESULTS.md).

<a id="fits"></a>

## 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 process**: Hundreds of thousands of JavaScript sessions, with nothing spent on the ones that aren't running.
- **Full control over the outside world**: A deterministic runtime, whose clock, randomness and messages all come from your application.
- **Untrusted code**: A sandbox for code your users or a model wrote.
- **The same code, many times**: Work 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.

<a id="others"></a>

## Each alternative gives one up

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

| Option | What it costs |
|---|---|
| [QuickJS in WebAssembly](https://tinyscript.tinyactors.dev/compare/quickjs-wasi/index.md) | A second engine around the first, to make it a sandbox. |
| [V8 isolates](https://tinyscript.tinyactors.dev/compare/isolates/index.md) | Too heavy for one per session, and not a security boundary alone. |
| [Ahead-of-time compilers such as scriptc](https://tinyscript.tinyactors.dev/compare/scriptc/index.md) | Native programs, not code you can safely run in your process. |

tinyscript aims at all four jobs, inside your process. The [comparison](https://tinyscript.tinyactors.dev/compare/index.md) also covers djs and Psychoscript, which aim at code mode too.

<a id="safe"></a>

## 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.

- **The host owns the buffers**: A host request is written into a packet buffer the host supplies, and the host answers it with a reply. Source: [tinyscript, `include/tinyscript.h`, `ta_tinyscript_set_host_buffers`](https://github.com/tinyactors-dev/tinyactors/blob/ef90baf70d8974072e9007c482190ec61043ce7c/tinyscript/include/tinyscript.h)

<a id="density"></a>

## 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`](https://github.com/tinyactors-dev/tinyactors/blob/ef90baf70d8974072e9007c482190ec61043ce7c/tinyscript/include/tinyscript.h)

<a id="fast"></a>

## 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`](https://github.com/tinyactors-dev/tinyactors/blob/ef90baf70d8974072e9007c482190ec61043ce7c/tinyscript/README.md)

<a id="early-days"></a>

## 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 mode**: 62.9% (31310 of 49767)
- **Language syntax and semantics**: 85.2% (18969 of 22259)
- **Built-in objects**: 51.2% (12130 of 23692)
- **Annex B, web compatibility**: 30.4% (89 of 293)
- **Internationalization (ECMA-402)**: 0.6% (22 of 3406)

test262 in strict mode at tinyscript's pinned commit, by [its own run](https://github.com/tinyactors-dev/tinyactors/blob/ef90baf70d8974072e9007c482190ec61043ce7c/tinyscript/test262/expected-failures.txt).

[Read the reference](https://tinyscript.tinyactors.dev/reference/index.md) · [Measure it yourself](https://tinyscript.tinyactors.dev/playground/index.md)
