Discussion on: It's time for Typescript Native

speculative profile image
Jeffrey Tao

"Typescript Native" is a bit of a strange thing to ask for, in my humble opinion, because it's roughly the same as compiling Javascript. Compilation processes roughly fall into two categories:

  1. "Ahead of Time" compilation to native machine code
  2. "Just in Time" compilation at run-time

Typescript and Javascript fall into the 2nd category. At run-time in every javascript engine, the source code is JITed before execution begins. As far as I'm aware, this is the most straightforward way to compile javascript, which may mutate its code during execution (such as with eval or require) -- new bits of code can simply be JITed during execution. Having these features means that the execution context must have access to a javascript compiler/interpreter.

So I think there are two things at play in your suggestion here:

  1. Emitting a single executable file
  2. Performance improvement

With respect to emitting a single executable file, if this is your only goal, you can accomplish it by generating an executable containing a Javascript runtime (like V8). There is precedence for things like this (such as py2exe) but they're normally concerned with portable distribution of software to consumers who may not have the relevant runtimes intstalled. Obviously, this is not a particularly satisfying solution and you're essentially just wrapping up the runtime with your source code as the data section of the executable.

As far as performance is concerned, initial JIT to start running can be significant (depending on the size of your program). Ahead of time compilation could potentially solve this, although there are multiple relative levels of performance cost at play: require() time (mostly due to synchronous file I/O), parse/compilation time. You can actually improve the require file I/O time drastically by using a javascript bundler (webpack, parcel), since it will combine all of your scripts into a single file.

Finally, I think it's important to consider that Typescript's stated goal is to be a strict syntactical superset of Javascript, so removal of standardized features in order to accommodate ahead of time compilation seems unlikely for the official Typescript project. There is asm.js, which is the subset of javascript which would probably allow for ahead-of-time compilation.