title: (Mis)adventures with JS Compilers author: name: Nathan Epstein twitter: epstein_n url: http://nepste.in email: _@nepste.in
--
--
- AI library for JS (github.com/nathanepstein/pavlov.js)
- Initially written in ES5
- ... then re-written in ES6 (compiled w/ Babel)
- ... then re-written in C++ (compiled w/ Emscripten)
--
--
-
Platform ubiquity (web and otherwise) of JS.
-
Features / syntax / libraries / etc. of other languages.
-
Performance optimizations (maybe) from the compiler.
--
--
- Compilers are used to write syntax in one language and convert it to another (possibly machine executable) computer language.
--
-
In some languages, high level syntax is compiled to a (possibly optimized) executable before runtime (ahead-of-time compiling).
-
Other languages (like JavaScript) are JIT (just-in-time) compiled. This is a hybrid of interpretation and compilation (for heavily used pieces of code).
--
--
|
Compiles ES6 to ES5 allowing for use of: |
|
|
|
-
Scala.js compiles Scala to optimized JS.
-
Several options for Haskell on their wiki under the "The JavaScript Problem".
--
--
In some cases, we can use a compiler to generate more performant JavaScript then what we are able to produce with "hand written" JS.
--
- layout of data in physical memory
- ambiguous functions
- garbage collection
- unoptimized code
--
-
In a typed language, elements of an array / vector will typically be stored in contiguous memory.
-
This is unlikely to happen in JS because array elements have different types / sizes.
--
--
- Functions with different argument types may have similar syntax but do very different things in physical memory (i.e.
let add = (a, b) => a + b;
).
--
- Heap memory is not manually managed; may be reclaimed at unpredictable / undesirable times.
--
- "Hand written" code may contain room for optimizations that a compiler can perform (i.e. unnecessary computations and intermediate values, result reuse, memory locality, etc).
--
-
Allows us to compile LLVM-to-JS (C, C++, Rust).
-
Can target WebAssembly or asm.js (optimizable low-level subset of JS).
-
asm.js may be AOT or JIT compiled depending on browser.
--
-
Pre-emptive checks for type and linking errors.
-
Libraries and syntax associated with the original language.
-
Sophisticated performance optimization that goes into the compiler.