Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions docs/internal/jit-compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Elysia is fast and will likely remain *one of the fastest web frameworks for Jav
<Benchmark />
</section>

Elysia speed is not only acheived by optimization for specific runtime eg. Bun native features like `Bun.serve.routes`. But also the way Elysia handles route registration and request handling.
Elysia speed is not only achieved by optimization for specific runtime eg. Bun native features like `Bun.serve.routes`. But also the way Elysia handles route registration and request handling.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentence is still grammatically awkward: consider using possessive and plural forms and avoiding the sentence break after “runtime”. For example, “Elysia’s speed is achieved not only through optimizations for specific runtimes (e.g., Bun-native features like …) but also through …”

Suggested change
Elysia speed is not only achieved by optimization for specific runtime eg. Bun native features like `Bun.serve.routes`. But also the way Elysia handles route registration and request handling.
Elysia’s speed is achieved not only through optimizations for specific runtimes (e.g., Bun-native features like `Bun.serve.routes`) but also through the way Elysia handles route registration and request handling.

Copilot uses AI. Check for mistakes.

Elysia has an **JIT "compiler"** embedded within its core since [Elysia 0.4](/blog/elysia-04) (30 Mar 2023) at (*src/compose.ts*) using `new Function(...)` or also known as `eval(...)`.
Elysia has a **JIT "compiler"** embedded within its core since [Elysia 0.4](/blog/elysia-04) (30 Mar 2023) at (*src/compose.ts*) using `new Function(...)` or also known as `eval(...)`.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“embedded within its core … at (src/compose.ts)” reads like a location mismatch; “in (src/compose.ts)” or “located at …” would be clearer.

Copilot uses AI. Check for mistakes.

The *"compiler"* is not a traditional compiler that translates code from one language to another. Instead, it dynamically generates optimized code for handling requests based on the defined routes and middleware. *(Which is why we put compiler in quotes.)*

When request is made to Elysia application for the first time for each route, Elysia dynamically generates optimized code specifically tailored to handle that route efficiently on the fly avoiding unnecessary overhead as much as possible.
When a request is made to the Elysia application for the first time for each route, Elysia dynamically generates optimized code specifically tailored to handle that route efficiently on the fly avoiding unnecessary overhead as much as possible.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is hard to read due to repeated phrasing (“for the first time for each route”) and missing punctuation around “on the fly”. Consider reordering to “The first time a request is made for a route, …” and adding a comma before/after “on the fly” to improve clarity.

Suggested change
When a request is made to the Elysia application for the first time for each route, Elysia dynamically generates optimized code specifically tailored to handle that route efficiently on the fly avoiding unnecessary overhead as much as possible.
The first time a request is made for a route in the Elysia application, Elysia dynamically generates optimized code specifically tailored to handle that route efficiently, on the fly, avoiding unnecessary overhead as much as possible.

Copilot uses AI. Check for mistakes.

## Static Code Analysis (Sucrose)

Expand All @@ -58,7 +58,7 @@ const app = new Elysia()

In this code, we can clearly see that this handler only need a `params` to be parsed.

Sucrose looks at code and tells the *"compiler"* to only parse **params** and skip parsing other parts of the request like **body**, **query**, **headers** entirely as it's not need.
Sucrose looks at code and tells the *"compiler"* to only parse **params** and skip parsing other parts of the request like **body**, **query**, **headers** entirely as it's not needed.
Comment on lines 59 to +61
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of grammar issues remain in this paragraph: “this handler only need” should be “only needs”, and “Sucrose looks at code” should be “looks at the code” for natural English.

Copilot uses AI. Check for mistakes.

JIT "compiler" then generates code like this:

Expand Down Expand Up @@ -127,13 +127,13 @@ When `set` or `status` is not used, Elysia will use `mapCompactResponse` to map

Elysia is originally made specifically for Bun but also works on [Node.js](/integrations/node), [Deno](/integrations/deno), [Cloudflare Workers](/integrations/cloudflare-workers) and more.

There are a big difference between being **compatible** and being **optimized** for a specific platform.
There is a big difference between being **compatible** and being **optimized** for a specific platform.

Elysia can take advantage of platform-specific features and optimizations to further enhance performance, for example `Bun.serve.routes` is used when running on Bun to leverage Bun's native routing capabilities which is written in Zig for maximum performance.

Using the **inline response** for maximum performance for static responses which made Elysia the rank at #14 on [TechEmpower Framework Benchmarks](https://www.techempower.com/benchmarks/#section=data-r23&hw=ph&test=plaintext) among the world's fastest backend frameworks.
Using **inline responses** for maximum performance on static responses has ranked Elysia at #14 on [TechEmpower Framework Benchmarks](https://www.techempower.com/benchmarks/#section=data-r23&hw=ph&test=plaintext) among the world's fastest backend frameworks.

There are more various smaller optimization like
There are various smaller optimization like
- using **Bun.websocket** when running on Bun for optimal WebSocket performance
- `Elysia.file` conditionally use `Bun.file` when available for faster file handling
- using `Headers.toJSON()` when running on Bun to reduce overhead when dealing headers
Comment on lines +136 to 139
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar/pluralization: this should be “various smaller optimizations”, and “like” reads better with a colon (“… optimizations like:”). Also, in the bullets below, verb agreement and phrasing need fixes (e.g., “conditionally uses …” and “when dealing with headers”).

Suggested change
There are various smaller optimization like
- using **Bun.websocket** when running on Bun for optimal WebSocket performance
- `Elysia.file` conditionally use `Bun.file` when available for faster file handling
- using `Headers.toJSON()` when running on Bun to reduce overhead when dealing headers
There are various smaller optimizations like:
- using **Bun.websocket** when running on Bun for optimal WebSocket performance
- `Elysia.file` conditionally uses `Bun.file` when available for faster file handling
- using `Headers.toJSON()` when running on Bun to reduce overhead when dealing with headers

Copilot uses AI. Check for mistakes.
Expand All @@ -147,7 +147,7 @@ Elysia JIT *"compiler"* is designed for peak performance in mind. However, the d
### Initial Request Overhead
The first time a request is made to a specific route, Elysia needs to analyze the route handler code and generate the optimized code.

This process is relatively **very fast** and usually takes < 0.005ms per route in most cases on a modern CPU and happend only **once per route**. But it is still an overhead.
This process is **very fast** and usually takes < 0.005ms per route in most cases on a modern CPU and happens only **once per route**. But it is still an overhead.

<JIT />

Expand All @@ -156,7 +156,7 @@ This process can be moved to the startup phase by settings `precompile: true` to
### Memory Usage
The dynamically generated code is stored in memory for subsequent requests. This can lead to increased memory usage, especially for applications with a large number of routes but is relatively low.

### Bigger Bundle Size
### Increased Bundle Size
The JIT "compiler" and Sucrose module add some additional code to the Elysia core library, which can increase the overall bundle size of the application. However, the performance benefits often outweigh the cost of a slightly larger bundle size.

### Maintainability
Expand All @@ -165,9 +165,9 @@ The use of dynamic code generation can make the codebase more complex and harder
### Security Considerations
Using `new Function(...)` or `eval(...)` can introduce security risks **if not handled properly**.

But that's only "if not handled properly" part.
But that's only true if not handled properly.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is redundant: the previous sentence already states the “if not handled properly” condition, and this line repeats it without adding new information. Consider removing it or replacing it with a concrete example of what “handled properly” means in Elysia’s case.

Suggested change
But that's only true if not handled properly.
In Elysia's case, "handled properly" means that generated functions are built only from trusted framework code (such as Sucrose and registered schemas), never directly from raw user input or request data.

Copilot uses AI. Check for mistakes.

Elysia takes precautions to ensure that the generated code is safe and does not expose vulnerabilities by make sure that only trusted code is executed. The **input is almost never user-controlled** and produced by Elysia (sucrose) itself.
Elysia takes precautions to ensure that the generated code is safe and does not expose vulnerabilities by making sure that only trusted code is executed. The **input is almost never user-controlled** and produced by Elysia (sucrose) itself.

## Libraries that `eval`
Elysia is not the only framework that use `new Function` and `eval`.
Comment on lines +170 to 173
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalization/grammar: “Elysia (sucrose)” should likely be “Elysia (Sucrose)”, and “the only framework that use” should be “frameworks that use”.

Suggested change
Elysia takes precautions to ensure that the generated code is safe and does not expose vulnerabilities by making sure that only trusted code is executed. The **input is almost never user-controlled** and produced by Elysia (sucrose) itself.
## Libraries that `eval`
Elysia is not the only framework that use `new Function` and `eval`.
Elysia takes precautions to ensure that the generated code is safe and does not expose vulnerabilities by making sure that only trusted code is executed. The **input is almost never user-controlled** and produced by Elysia (Sucrose) itself.
## Libraries that `eval`
Elysia is not the only framework that uses `new Function` and `eval`.

Copilot uses AI. Check for mistakes.
Expand Down
Loading