Recently open-sourced BuckleScript, a JavaScript backend for OCaml, aims to bring OCaml type safe, high performance code to any JavaScript execution engine, Bloomberg say.

BuckleScript, originally released under the name of OcamlScript then renamed to avoid confusion with another project, has been developed at Bloomberg Labs and aims to enable large scale JavaScript programming. According to Bloomberg, this is made possible by combining OCaml’s industrial strength type system with a fast compiler and offline, i.e., compile time optimizer.

BuckleScript was inspired by js_of_ocaml, according to its main authors, but it has different goals. In particular, BuckleScript is designed to help easier integration with existing JavaScript system, which means that it is possible to call a function defined in an BuckleScript from a plain JavaScript module. This is made possible by the fact that each BuckleScript module is translated into an ES6 module, a concept known as separate compilation that is also available in TypeScript, and that no name mangling is used, which also makes it possible to generate debuggable JavaScript.

As an example of that, the following OCaml code:

let sum n = let v = ref 0 in for i = 0 to n do v := !v + i done; !v

is translated into:

function sum(n) { var v = 0; for(var i = 0; i<= n; ++i){ v += i; } return v; }

BuckleScript can in certain cases outperforn carefully implemented JavaScript libraries, Bloomberg’s engineers say. In particular, a BuckleScript implementation of Facebook’s immutable library shows a twofold performance improvement and a drastic reduction in code size.

Still in its early stages, BuckleScript is available both for Linux and Mac OS X, while Windows support is in the workings. Its source code is available on GitHub. It can be also tried out through its web-based REPL.