Skip to content

JIT Compiler

Byte includes a JIT compiler for inlining method calls.

Here is an example application:

const app = new Byte()
.use(f1)
.defer(f2)
.get('/', f3);

The route will be stored as:

const route = {
method: 'GET',
path: '/',
handler: f3,
actions: [[[2, f1]]]
defers: [[f2]]
};

With the JIT compiler the route execution can be simplified to:

async function run(ctx: Context) {
// Run middlewares
const c1 = await f1(ctx);
if (c1 instanceof Response) return c1;
let r = await f4(ctx);
const c2 = await f2(ctx);
if (c2 instanceof Response) r = c2;
return r;
}

This reduces the amount of unnecessary checks the handlers need to do at request time.

The compiler can optimize even more in these cases:

  • If a function has no parameter then do not pass ctx as an argument.
  • If a function is not asynchronous then remove await.
  • If no functions have parameters then the run function does not need ctx as a parameter.
  • If no functions are asynchronous then the run function can be synchronous.

The source code of the compiler can be seen here.

Route matching

The route matching phase uses the Blitz router, which compiles the routes into an optimized matching function. You can skip JIT compilation for this phase by using EdgeRouter.

import { EdgeRouter } from '@bit-js/blitz';
// Build the fetch function with EdgeRouter
app.build(new EdgeRouter());