Tech At Bloomberg

BuckleScript: The 1.0 release has arrived!

September 08, 2016

BuckleScript is an optimizing compiler which compiles OCaml into JavaScript module by module without name mangling. The generated code is highly readable and super small, see the Hello world example and examples of calling JS libraries. BuckleScript is strongly inspired by TypeScript, BabelJS – we consider readability and easy integration with existing JS libraries to be essential.

There are a few things we believe BuckleScript does better than TypeScript:

Generated code is more efficient and fully type-safe

  • BuckleScript is not a new language, it is OCaml. OCaml offers an industrial-strength state-of-the-art type system and provides very strong type inference (i.e. unlike TypeScript, no verbose type annotation is required), which proves invaluable in managing large projects.
  • OCaml’s type system is not just for tooling; it is a sound type system which means it is guaranteed there will be no runtime type errors after type checking. Based on this guarantee, BuckleScript provides many optimizations during offline compilation. You can view a simple benchmark here!

All the benefits of OCaml with a feature rich and efficient Foreign Function Interface (FFI)

  • Your program can be compiled into native code, JavaScript, or even a unikernel. The BuckleScript compiler is expected to become even faster and produce better code due to more and more optimizations in the upstream OCaml compiler.
  • Thanks to OCaml’s native support of the Object structural type system, BuckleScript’s FFI can model JavaScript objects natively. Some powerful type system features like polymorphic variants and GADTs are also invaluable in modeling existing JavaScript libraries.
  • Unlike most language FFIs, the design goal of our FFI is to avoid writing any unsafe JS “stub” code with a performance cost. In particular, we try to avoid any unnecessary memory allocations in the FFI.

Compilation speed is key to a good developer experience

  • Our internal benchmarks show that BuckleScript compiles at least ten times faster than TypeScript. There is no magic here, the BuckleScript compiler is written in OCaml which is compiled to blazing fast native code.
  • However, this does not limit the availability of the compiler: it can also be compiled to a JavaScript program (700KB for pure OCaml + 300KB for Reason support).
  • The playground is powered by the compiler compiled to JavaScript, which demonstrates a nearly real-time compilation speed. Imagine how fast the native compiler is!

We are pleased to announce a stable 1.0 release of BuckleScript, which covers the whole OCaml language (except some features highly coupled with runtime, like the GC and Unix modules, etc.). To learn more about BuckleScript, please refer to the user manual. We welcome your feedback.