Performance

We have compared the running time of OCaml programs executed natively, interpreted by the bytecode interpreter, and executed by a JavaScript engine after compilation by Js_of_ocaml.

With a state-of-the-art JavaScript engine (such as Google's V8 and Mozilla's SpiderMonkey), programs often run faster when compiled to JavaScript than with the OCaml bytecode interpreter.

Benchmarks were run in November 2022 with engines available in Ubuntu 22.04 (V8: 8.4.371.19-node.16, SpiderMonkey: JavaScript-C91.10.0).

Execution time

Relative execution times

Notes:

Code size

Relative size

Most of the time, the generated JavaScript code is smaller than the bytecode file. The benchmarks are small programs that do not make much use of any library. The generated code remains smaller than the bytecode file for large programs (ocamlc, js_of_ocaml).

The size gain can be much larger for programs that rely on external libraries, thanks to dead code elimination.

Comparison with handwritten JavaScript

Relative execution times

Comparing execution times between handwritten JavaScript and generated code shows that results are implementation-dependent.

Compiler optimization levels

Relative size
Relative execution times

Effect handlers impact

Relative size
Relative size (compressed with bzip2)
Relative execution times

The performance impact of supporting effect handlers:

Size:

Speed: