Generate javascript code optimized for boot and JIT with Haxe

In recent years, JavaScript has become a very fast language thanks to the JIT (Just In Time compiler) that allow to analyze and optimize code at runtime during program execution.
That optimization doesn’t start immediately, since the JIT must be “burned” in order to optimize the code.
If we need to write programs that have a short life cycle and are invoked often or need immediately of the maximum performance (shell programs, widgets or banners) JIT can’t help to achieve maximum speed.
In this case we can try to use languages or tools that optimize the code generation.
For example Closure of google in Advanced mode allows to do inline functions and dead code elimination.
Haxe instead is a language that compiles toJavaScript (and in 10 other languages) that has a number of features to generate highly optimized code for startups and more easily “digested” by the JIT.
Here some features:
Static Type System
Haxe has a static type system as Typescript, Java … that allows to generate easily optimized code for the JIT.
This is because even though the type system “evaporate” leave an imprint or schema in the code.
The variables will not be reassigned to other types, and functions will always be recalled respecting the signature and argument types.
These checks made by the type system generate code more easily digested by the JIT.
Dead Code Elimination
Having a static type system and knowing the whole program structure, Haxe can delete all the functions and classes of loaded libraries as dependencies that have not been used.
For example, imagine that we have a very complex library that contains hundreds of classes, and of these we just have to call a static method of one of these. In this case the compiler will insert in the code generated only this function and other functions called inside his body.
This allows you to generate javascript files with little weight.
Inline functions
By defining a function inline Haxe will replace all the function call with his body.
This very optimizes the speed of execution, as each call to a function has a weight in terms of time. (imagine calling a function inside a loop).
A very powerful feature of inlining is to make also inline anonymous functions.
For example, if you pass an anonymous function to a method such as map, the body of this will be inserted in the inlined map method.
Inline constructor
Sometimes you have to use objects only in a local context, without the need to pass its to other functions.
Defining an inline constructor Haxe will not generate a new object, but will rewrite his properties as variables of the local scope.
Static Analyzer
Haxe has a powerful static analyzer that can more optimize the code in a number of cases:
- Unused variables
- Assignment to superfluous temporary variables
- Generation of unused scopes
- Deletion of the unused branchs in if and switch
- Inline values and code replacements as these refer to immutable values deduced at compile time.
For example var a = 1, b = 2, c = a b; will become 3.
Macro
Macros are one of the more esoteric features of Haxe, that exit from the scope of the program for enter in the metaprogramming.
Through the macro the compilers can learn new mode for generate code and make our code writing more DRY (Don’t repeat Yourself).
Unlike other languages, the code generated by the macro will always be validated by the type system giving an additional level of control over the code generated.