# Optimizations

Each section is one choice: what Lithe does, what the other way would cost, the number that backs it, and what it asks of you. Figures come from
[Performance](/docs/reference/performance), which has the method and the [limits](/docs/reference/performance#limits). They are medians from one machine and
move by **10 to 20%** from one session to the next, so a gap smaller than that isn't a result. A few come from the side by side run made when a choice was
taken, and say so.

Three examples of the repository come back often, so here is what they are. `todo` is a to-do list page with a form, a list and a few counters. The
`markup` example is a small page with one clicked counter, the smallest client of the examples that has one. `hello` is the smallest app.

## Nothing ships for what can't change

![What a static page, a page with islands and a page that hydrates whole ship](/docs/charts/what-ships.svg)

The compiler reads what each component asks of the browser: an event, a binding, `mount`, `effect`, `@await`, a browser API, a state declared in a `.kt`
file. A component that asks for none is rendered at build time and never reaches the client. There is no setting, the page gets what follows from its `App`:

- **A static page** gets no `app.mjs`. The `static` example ships **0 B** of JavaScript and 185 B of HTML with Brotli. The browser code isn't even compiled,
  which skips the release link, [most of the time of a build](#build-and-tooling-speed). A static build on its own isn't timed.
- **A page with islands** gets the code of the islands only. `App` and everything above them is never hydrated.
- **A page that needs the browser at its root** hydrates whole: **17.5 KB** of JavaScript with Brotli for `todo`.

Hydration adopts the nodes of the server's HTML instead of building them again, and the server writes no marker on them: the client matches the tree by
walking it. Solid's page needs an inline script to hand the events that come before hydration to its client, which makes its HTML 957 bytes with Brotli
against 749 for Lithe. On `todo`, hydration takes 8.7 ms for Lithe, 9.1 ms for Svelte and 9.1 ms for Solid on an unthrottled CPU, ranges that overlap, so
there is no winner. On a table of 1,000 rows Lithe is behind Svelte, **20.1 ms against 11.1 ms**, and close to Solid (17.9 ms): it creates one effect per
binding, where Svelte groups the updates of a row.

**What it costs you**

- An island is the outermost component that needs the browser, so islands don't nest. A function, markup with parameters or a rest prop can't cross to one:
  the component that gives it needs the browser itself, see [Static pages and islands](/docs/sites/islands).
- Props reach an island as JSON. A `String`, a number, a `Boolean` and lists of those cost nothing. Any other type brings kotlinx.serialization, about
  **43 KB** more in the client.
- A state that an island changes isn't seen by a component rendered at build time. Two islands share one through a variable of a `.kt` file.
- Hydration needs the HTML to parse back to the tree that was rendered: a `<table>` holds its rows through a `<tbody>`, and code that replaces the first or
  last element of a list item breaks the list.

## A component runs once

A component is a function that runs one time. Each text, attribute or block that can change is a small lambda subscribed to the state it read, and only
that lambda runs again. There is no virtual DOM to diff and nothing to memoize.

![Reactivity graphs, Lithe, Svelte and Solid](/docs/charts/reactivity.svg)

These are the graphs of Svelte's own benchmarks, written with the public API of each library. Lithe has the lowest time on all ten. The gaps under 20%, like
`mux` against Solid, aren't results. The widest are `deep` (**2.38 ms** against 4.40 for Svelte and 5.15 for Solid) and `wide` (0.50 ms against 1.00 and
1.67). The signals link each source to its readers through linked lists, the way alien-signals does: a run that reads what the last one read allocates
nothing. On a table of 10,000 rows, selecting a row took **179 ms** with arrays and **20 ms** with links (measured on 2026-11-26, nothing else changed).

**The selector.** Selecting a row is the case where the compiler helps most. In a keyed `@for`, a comparison of the key with a state runs once per row on
Svelte and Solid, and for two rows on Lithe:

![A row selected: every row runs on Svelte and Solid, two rows run on Lithe](/docs/charts/selector.svg)

```html
<script kotlin>
	prop val rows: List<Row>
	var selected by state(0)
</script>

@for (row in rows, key = row.id) {
	<tr class={"danger" on (selected == row.id)} @click={selected = row.id}>
		<td>{row.label}</td>
	</tr>
}
```

The compiler writes the comparison as a call of a selector declared once before the loop:

```kotlin
val __selector0 = keySelector { selected }
"danger" on (__selector0(row.id))
```

**Moving rows.** A reordered list keeps the rows that didn't move, then moves only the ones outside the longest run that is still in order, with
[`moveBefore`](https://developer.mozilla.org/docs/Web/API/Element/moveBefore) where the browser has it.

| 1,000 rows       |  Lithe |  Svelte |  Solid |
|------------------|-------:|--------:|-------:|
| Reverse the list | 2.3 ms | 30.3 ms | 2.0 ms |
| Swap two rows    | 0.5 ms |  0.6 ms | 0.4 ms |

`moveBefore` leaves more of the work to layout, which these times leave out: with it, a reversal took about 40 ms on the Lithe page on 2026-11-28.

**Two smaller choices, measured side by side.** Rows that hold only elements and texts are cloned from a template: creating 10,000 rows took **92.0 ms**
element by element and **71.8 ms** cloned. The `by` variables of a component use inline operators, so Kotlin/JS builds no property object for each read:
selecting a row of 10,000 took 9.4 ms with them and **6.4 ms** without, hydrating 1,000 rows 18.7 ms and 14.9 ms.

**What it costs you**

- A plain `val` that reads state keeps its first value, since the script runs once. The compiler warns, and a value that follows state is written
  `val a by derived { }`.
- Creating rows is slower than on Svelte: creating 1,000 rows takes 9.1 ms against 6.2 ms, and 10,000 rows 69.2 ms against 60.1 ms.
- A row that holds a component, a block or a spread isn't cloned, it is built element by element.
- Where the browser has no `moveBefore`, a row that moves loses its focus.

## Two outputs, and a client that carries what it uses

![One .lithe file becomes an HTML writer for the JVM and DOM code for the browser](/docs/charts/two-outputs.svg)

The compiler writes two outputs per component. The JVM one builds an HTML string and leaves out everything that only runs in a browser, which the compiler
finds by itself. A page of 1,000 rows (186,980 characters) renders in **1.28 ms** (10th to 90th percentile 1.23 to 1.39 ms), and compiling the 55 components
of the examples takes 19.1 ms. Rendering needs no browser: Kobweb's own static export runs the app in a headless browser instead, which isn't measured here.

The JS output holds everything, and the link keeps only what the components call. It drops `@for`, `@await`, `<head>` and the others when nothing uses them,
and the runtime's browser code avoids the parts of the Kotlin library that are large for what they do: `Regex`, `sortBy`, the text functions of `String`
and function references like `::dispose`, which bring reflection support. The size of the client follows the blocks of the page:

![JavaScript of each example, compressed with Brotli](/docs/charts/example-sizes.svg)

`effects` ships 6.4 KB, `lists` 16.6 KB, `flow` 16.8 KB and the `tasks` app 31.0 KB, with Brotli. `compose` mounts a composable, which brings the Compose
runtime: **129.1 KB**. The runtime doesn't skip the space normalizing of class lists, and doesn't compute custom properties with one join, because 60
interleaved loads each showed no change in hydration.

**What it costs you**

- The compiler reads one file at a time, so a helper declared in a `src@js` Kotlin file isn't recognized as browser code. The Kotlin compiler reports its
  use in static code.
- An `@await` expression that needs a browser API isn't possible, since static rendering runs it: declare the loader once in `src@jvm` and once in
  `src@js`.

## Kotlin/JS and its own runtime, not Compose in the browser

![Load and click times of the todo page on a CPU slowed 4 times](/docs/charts/todo-load.svg)

The same `todo` page written with Svelte and with Solid, both rendered on the server and hydrated, and a reference build on a runtime made with Compose. The
CPU is slowed 4 times, and the last column is on Chrome's slow 4G preset:

| Build   | JavaScript, Brotli | Hydrate  | Click   | Ready on slow 4G |
|---------|-------------------:|---------:|--------:|-----------------:|
| Lithe   |          17.5 KB   |  47.5 ms | 10.8 ms |         556.3 ms |
| Svelte  |          22.0 KB   |  48.0 ms | 12.9 ms |         557.3 ms |
| Solid   |          20.5 KB   |  48.4 ms | 10.1 ms |         553.7 ms |
| Compose |         119.7 KB   | 194.1 ms | 64.0 ms |       1,174.1 ms |

The Compose client is about **7 times** larger and hydrates about **4 times** slower. On a table page, creating 1,000 rows takes 511.0 ms against 9.1 ms, and
10,000 rows weigh 121.6 MB of heap against 11.4 MB. Compose HTML renders into an empty element in the browser only: it can't render on the JVM or adopt
existing HTML, so it composes the page again. That is a different job from hydrating, and the [Limits](/docs/reference/performance#limits) say how fair the
match is. Lithe, Svelte and Solid are close here: Lithe's client is the smallest, and the hydration times overlap.

Kotlin/JS rather than Wasm isn't measured, because there is no Wasm build of Lithe to compare with. The choice rests on the reasons in
[How it works](/docs/how-it-works), not on a number.

**What it costs you**

- The browser side is JavaScript, for browsers of Baseline early 2025 or newer. There is no polyfill and no fallback.
- Compose comes only where you ask for it: a [composable](/docs/sites/compose) mounted in a page brings its runtime.

## The production link

![What the minification of a site build does to the todo client](/docs/charts/size-journey.svg)

A site build links the whole program, removes dead code, then runs esbuild. The toolchain's own link writes one unminified file per Kotlin file, so Lithe
links directly. Raw, the `todo` client goes from **177.1 KB** as linked to 57.6 KB with esbuild alone and **55.5 KB** as the site build writes it. These were
measured on 2026-11-28, and the client of `todo` weighs 2,006 bytes more on the 2026-11-30 figures above.

- The link flags make a `Long` a JavaScript `bigint`, so the classes that emulate 64-bit integers aren't shipped, give members short names and write a
  lambda where it is used. Together they took the `todo` client from **72.4 KB** to **60.4 KB** minified (2026-11-26).
- The build removes the polyfills of the ES2015 link, since every Baseline browser has them, and the property objects Kotlin/JS builds for `by` variables,
  and it shortens the members Kotlin renames. That is the 2.0 KB raw (0.6 KB with Brotli) between esbuild alone and the site build.
- esbuild is chosen over SWC for its speed, for about 2 KB of difference in output size. The speed gap isn't measured here.

**What it costs you**

- The first site build downloads the esbuild binary (about 4.8 MB) and checks it against the registry checksum. It needs the network once, and neither Node nor
  Bun.
- A `Long` is a `bigint` in the browser.
- A polyfill the build doesn't know fails the build, rather than being dropped.
- A site ships no source map, and the dev server doesn't minify.

## Build and tooling speed

![Phases of a site build in a warm JVM](/docs/charts/build-phases.svg)

The release link is most of a build: **2,590 ms** of 3,114 ms on `markup`, and 2,792 ms of 3,637 ms on `todo`. The JVM output compiles and renders while the
client compiles, links and is minified, so the phases add up to more than the whole. A `cli` command starts a JVM that compiles cold what changed:
**22.29 s** after an edit of the markup of a component on `todo`, against the 3.6 s above in a warm JVM. Builds keep their steps in a cache addressed by a
hash of their inputs, so a build with nothing changed takes 4.85 s, JVM start and static rendering included.

![Time from a save to the event the browser receives, by kind of edit](/docs/charts/dev-server-edits.svg)

The [dev server](/docs/sites/dev-server) stays warm and compiles incrementally, and it avoids the compile when it can. The Kotlin of a development build reads
the texts, the comments, the static attributes and the static HTML of the markup from tables, so an edit of those only runs the Lithe compiler:
**170 ms** for a text of the markup against **2.46 s** for an expression, about 14 times less. The style block of a component swaps its stylesheet without a
reload, and a stylesheet of `resources/` needs no build. On `hello`, the smallest app, a Kotlin file takes 1.23 s. The time includes the 50 ms of debounce.

The [language server](/docs/tooling/editors) answers errors after a change in 42 ms, completion in 2.7 ms and highlighting in 0.6 ms, with Java 25. The
first answer after a start takes 349 ms.

**What it costs you**

- A change of the markup expressions, the script or Kotlin reloads the page and loses its state. Only a stylesheet swaps in place.
- The first build of a server takes **32.22 s** on `todo` and 28.63 s on `hello`, and a module has to be built once with the toolchain first.
- The dev server builds through the Kotlin Build Tools API, which is experimental, so its version is pinned to the one of Kotlin.

## How these were measured

Every figure above is on [Performance](/docs/reference/performance), with the machine, the versions, the number of samples and the ranges: one machine,
Chrome only and headless, medians that move by 10 to 20% between sessions. Its Limits list what the figures don't say, among them that the sizes of the
table pages can't be compared and that the Compose pages aren't a fair match. A figure here that isn't on that page comes from the side by side run made when
the choice was taken, and is dated. What isn't measured: Kotlin/JS against Wasm, esbuild against SWC for speed, the build time of a static page on its own,
and any browser other than Chrome.

The charts are drawn by `benchmarks/harness/charts.ts` from the files of `benchmarks/harness/results`.
