Porffor: An ahead-of-time JavaScript-to-native/WASM compiler

goranmoomin1 pts0 comments

Porffor

An ahead-of-time JavaScript compiler

Porffor is a research project that compiles JavaScript ahead-of-time to WebAssembly and native binaries.

Porffor

P50

16.3ms

P90

27.5ms

P99

41.1ms

Node

P50

194ms

P90

256ms

P99

323ms

new! Cold start times for an AWS Lambda hello world function (source and methodology)

-->

new!This page is served by a Porffor-compiled TS binary!

[live stats]<br>memory<br>binary<br>requests<br>uptime

JS -> Wasm

Porffor's WebAssembly output is<br>10-30x smaller and faster compared to existing JS -> Wasm<br>projects as Porffor compiles JS instead of bundling an interpreter.

JS as Wasm allows for sandboxed execution but suffers drastic<br>performance losses: Porffor solves this , allowing for:

Secure, efficient server-side JS hosting

For edge runtimes, Wasm sandboxing allows secure execution without<br>needing excessive isolation; combined with AOTs low overhead<br>compared to JITs, Porffor could allow for running many more<br>customers on the same hardware with minimal performance loss.

Reverse-engineering resistance

If you have especially sensitive JS, compilation can be much harder<br>to reverse than code obfuscation.

JS -> Native

As Porffor truly compiles JS without packaging a runtime,<br>binary sizes are up to 1000x smaller (~90MB -><br>JS as native allows for simple one-click execution but has substandard<br>sizes: Porffor solves this , allowing for:

Fast JS for embedded, game consoles, and more

Under the hood, Porffor compiles JS -> C -> native; anywhere you can<br>use C, you can now use JS!

Tiny CLI apps written in JS

Imagine writing a CLI app in JS and compiling it to a one-click<br>executable under 1MB!

Porffor has safe compilation as it is written in JS (preventing memory safety vulnerabilities) with 0 eval.

Porffor is written from scratch with ahead-of-time in mind allowing for previously impossible optimizations.

Porffor natively supports TypeScript , no build step: just give it a TS file.

Compile-time (developer's machine)

Traditional embedded compiler<br>┌──────────────┐ ┌────────────┐<br>│ │ │ │<br>│ JavaScript ├─────►│ Bytecode ├─────────────────────────────────►<br>│ │ │ │<br>└──────────────┘ └────────────┘

Porffor (AOT compiler)<br>┌──────────────┐ ┌──────────────────────┐<br>│ │ │ │<br>│ JavaScript ├─────►│ Binary (Wasm/native) ├───────────────────────►<br>│ │ │ │<br>└──────────────┘ └──────────────────────┘

Runtime (user's machine)

Interpreter<br>┌──────────────┐ ┌────────────┐ ┌─────────────┐<br>│ │ │ │ │ │<br>│ JavaScript ├─────►│ Bytecode ├─────►│ Evaluated │<br>│ │ │ │ │ │<br>└──────────────┘ └────────────┘ └─────────────┘

Just-in-time compiler (JIT)<br>┌──────────────┐ ┌──────────────┐ ┌────────────┐<br>│ │ │ │ │ │<br>│ JavaScript ├─────►│ Machine code ├─────►│ Executed │<br>│ │ │ │ │ │<br>└──────────────┘ └──────────────┘ └────────────┘

Browser JS engine<br>┌──────────────┐ ┌─────────────┐<br>│ │ Interpreter │ │<br>│ JavaScript ├───────┬──────►│ Evaluated │<br>│ │ │ │ │<br>└──────────────┘ | └─────────────┘<br>│ ┌─────────────┐<br>│ JIT Tier 1 │ │<br>└───────┬─────►│ Evaluated │<br>│ │ │<br>| └─────────────┘<br>│ ┌─────────────┐<br>│ JIT Tier 2 │ │<br>└───────┬─────►│ Evaluated │<br>│ │ │<br>... └─────────────┘

Traditional embedded interpreter<br>┌─────────────┐<br>│ │<br>│ Evaluated │<br>│ │<br>└─────────────┘

Wasm runtime or native machine<br>┌────────────┐<br>│ │<br>│ Executed │<br>│ │<br>└────────────┘

Instead of a slow interpreter or multiple JIT tiers carefully balancing compile speed (startup/loading time) and JS performance, compiling AOT allows you to compile first and run later. While compile speed for AOT remains important for DX, it no longer effects UX; allowing Porffor to perform static analysis for optimizations like C++ and Rust do today.

The main drawbacks of AOT is lack of dynamic JS evaluation (like eval) and needing to reinvent the JS engine, so as Porffor is early a lot of JS doesn't work yet, but it is improving by the day:

Test262

Porffor is run against Test262, the official ECMAScript conformance test suite, every commit to track conformance progress.

porffor javascript time native wasm compiler

Related Articles