In this post I want to test if there is any benefit to combining Prepack with the Closure compiler (ADVANCED_OPTIMIZATIONS).
Examples of such optimizations include unrolling loops and evaluating if statements.
This seems somewhat related to Closure compiler optimizations, but the difference is more focus on runtime performance than code size.
I wonder though, can I combine the two and get the best of both worlds?
Let's see what happens if we run both of them against the following code sample:
Here I have created a trivial program consisting of two loops with conditionals.
Let's start by running the Closure compiler on the program.
The result is pretty good as seen below:
As you can see, the functions have been inlined, and the conditionals have been converted to boolean expressions. Overall this is both compact and fast code.
Next I will Prepack the same code sample.
As you can tell the code is more verbose, but at the same time, simpler.
There is no longer any loops or conditionals in the code.
However, the code executes a lot of unnecessary statements like assignments to “i”. It also initializes cars and country arrays that are irrelevant to the outcome of the program.
One of the strengths of Closure compiler is detecting code without impact on the outcome of a program.
Here I have plenty of examples of unnecessary code.
Crazy idea! Let's run the Closure compiler on the output of Prepack to see what we get...
The result is pretty nice..
Closure compiler was able to determine that the outcome of this program is always two simple console log statements.
I have put this code on Github if you are interested in checking it out.