Are you missing out by not having native performance on your code?
That’s a fact.
But can it be compiled? That’s a whole different deal. Compiling a dynamic language ahead of time (what is also known as AOT or Ahead-Of-Time compilation) is possible, yes, but is it worth it?
AOT vs JIT
Let me quickly go into a bit more detail about AOT and JIT, because otherwise, this article would just be too short (and well, these are relevant topics that you might find useful as well).
The main difference between AOT and JIT is clearly in the moment they take place. While AOT is done before the execution of your code, JIT happens during that same execution. What else is different?
Almost everything else really.
AOT is usually meant for statically typed languages because for them, there is no dynamic behavior that needs to be examined and determined during execution. All the rules are laid out right there in the code, so the compiler can read it, understand how data flows internally and optimize accordingly, while at the same time, translating the code into a native interpretation (also known as machine code).
The JIT compiler, on the other hand, is meant to be used with dynamically typed languages, because it takes care of monitoring your code’s execution and based on the type of data it’s handling, it’ll optimize and create better machine code.
If you think about both cases in terms of Time-To-Optimize the compiled code, the AOT way will give you an optimized version of your code from the start. You’ll start your execution with an optimal version of it. While on the other hand, your JIT’ed code will take a while to pick-up speed, but it can potentially go further and better because as you can read here, there are other aspects to optimized during runtime than just the type definitions (aspects such as function calls can only be analyzed during execution).
There are definitely pros and cons for each one, but if I had to summarize and decide which one was better, I’d say:
- Go for an AOT alternative if your code is meant to run for a short time.
- Go for the JIT version on the cases when your code runs for longer periods, thus allowing for potentially better optimizing after some runtime analysis is done.
Can you really know the type of the result variable ahead of time? You probably need to take into consideration all potential types and dynamically define different alternatives that will adapt during runtime. However you solve it, you’d be adding a lot of logic on top of the execution, just to be able to execute it. That doesn’t sound too performant.
There is a project however, that is trying to achieve this, despite not being ideal (at least on paper): NectarJS
Compiling with NectarJS
According to their results, they have already made some improvements on Windows using Node.js (v12). Not necessarily improving speed in some situations, but rather the memory footprint and even the output file-size.
Granted, the project stills has its limitations, especially the fact that as of now, it only supports around 80% of ES3, which means the JS you can write is very limited and not up to today’s standards.
But then again, it might not be required for you to write ES6-compatible code for your particular project, and having the ability to compile it and run it natively on your Arduino board might come-in very handy.
Installing and testing NectarJS
The project can be installed directly as an NPM module, so all you have to do, is run the following line (assuming you already have Node installed, of course):
$ npm install nectarjs -g
After installation, and provided you have the requirements installed, you can write a basic HelloWorld example and compile it:
To compile it, simply use the following command:
$ nectar your-file.js
Here is the output I get when I run it on my OSX:
Notice the file created without an extension, that is the binary file. If you give it execution permissions, you’re ready to execute it. That simple, and it works.
Personally, I wouldn’t bet on it. The project itself seems to still be at its early age, having its documentation unfinished, and only partial support for an older version of the language. However, it’s under active development and these things can change very soon.
Would you consider compiling your JS code? Leave a comment with your thoughts!
Bit makes it simple to author, document, and share independent components across web projects.
Use Bit to maximize code reuse, speed-up delivery, and build apps that scale.
Bit supports Node, TypeScript, React, Vue, Angular, and more.
- Introduction to Aleph - The React Framework in Deno