Prepack is a tool that optimizes JavaScript source code:
Computations that can be done at compile-time instead of run-time get eliminated.
Prepack replaces the global code of a JavaScript bundle with equivalent code that is a simple sequence of assignments. This gets rid of most intermediate computations and object allocations.
Note how most computations have been pre-initialized. However, the function that computes four (_5) remains in the residual program since it was not called at initialization time.
A few things have to come together to realize Prepack:
</p>
<ul>
<li>
<b>Abstract Syntax Tree (AST)</b>
<p>
Prepack operates at the AST level, using <ahref="https://babeljs.io/">Babel</a> to parse and generate JavaScript source code.
</p>
</li>
<li>
<b>Concrete Execution</b>
<p>
At the core of Prepack is an almost ECMAScript 5 compatible interpreter — implemented in JavaScript!
The interpreter closely follows the <ahref="http://www.ecma-international.org/ecma-262/7.0/">ECMAScript 2016 Language Specification</a>, with a focus on correctness and spec conformance.
You can think of the interpreter in Prepack as a clean reference implementation of JavaScript.
</p>
<p>
The interpreter has the ability to track and undo all effects, including all object mutations. This enables speculative optimizations.
In addition to computing over concrete values, Prepack's interpreter has the ability to operate on <b>abstract values</b> which typically arise from environment interactions. For example, <code>Date.now</code> can return an abstract value. You can also manually inject abstract values via auxiliary helper functions such as <code>__abstract()</code>. Prepack tracks all operations that are performed over abstract values. When branching over abstract values, Prepack will fork execution and explore all possibilities. Thus, Prepack implements a <ahref="https://en.wikipedia.org/wiki/Symbolic_execution">Symbolic Execution</a> engine for JavaScript.
Symbolic execution will fork when it encounters branches over abstract values. At control-flow merge-points, Prepack will join the diverged executions, implementing a form of <ahref="https://en.wikipedia.org/wiki/Abstract_interpretation">Abstract Interpretation</a>. Joining variables and heap properties may result in conditional abstract values. Prepack tracks information about value and type domains of abstract values.
</p>
</li>
<li>
<b>Heap Serialization</b>
<p>
At the end of the initialization phase when the global code returns, Prepack captures the final heap.
Prepack walks the heap in order, generating fresh straightforward JavaScript code that creates and links all objects reachable in the initialized heap. Some of the values in the heap might be result of computations over abstract values. For those values, Prepack generates code that performs those computations as the original program would have done.
Out of the box, Prepack does not fully model a browser or node.js environment: Prepack has no built-in knowledge of <code>document</code> or <code>window</code>. In fact, when prepacking code which references such properties, they will evaluate to <code>undefined</code>. You would have to insert a model of the relevant functionality at the beginning of the code you want to prepack.
// A call to __optimize along the global code instructs Prepack to optimize the given function.
__optimize(global.f);
</code></pre>
<p>
<b>Note:</b> Prepack makes a significant assumption:
The optimized function (and all other functions it might transitively call) does not depend on any state that is mutated after the global code finished executing. Also, Prepack does check and reject two different optimized functions that mutate the same state.
<li>Assuming ES6 support for certain features, delay / ignore application of <ahref="https://en.wikipedia.org/wiki/Polyfill">Polyfills</a></li>
<li>Implement further compatibility targets, in particular the web and node.js</li>
<li>Deeper Integration with a JavaScript VM to improve the heap deserialization process, including
<ul>
<li>expose a lazy object initialization concept — lazily initialize an object the moment it is touched for the first time, in a way that is not observable by JavaScript code</li>
<li>efficient encoding of common object creations via specialized bytecodes</li>
<li>splitting the code into two phases: 1) a non-environment dependent phase; the VM could safely capture & restore the resulting heap; followed by 2) an environment dependent phase which patches up the concrete heap by performing any residual computations over values obtained from the environment</li>
<li>...</li>
</ul>
</li>
<li>Summarizing loops and recursion</li>
</ul>
<h3>Long Term — leveraging Prepack as a platform</h3>
<ul>
<li>JavaScript Playground — experiment with JavaScript features by tweaking a JavaScript engine written in JavaScript, all hosted just in a browser; think of it as a "Babel VM", realizing new JavaScript features that cannot just be compiled away</li>
<li>Effect Analyzer, e.g. to detect possible side effects of module factory functions or to enforce pureness annotations</li>
<li>Type Analysis</li>
<li>Information Flow Analysis</li>
<li>Call Graph Inference, allowing inlining and code indexing</li>
<li>Automated Test Generation, leveraging the symbolic execution features in combination with a constraint solver to compute inputs that exercise different execution paths</li>
<li>Smart Fuzzing</li>
<li>JavaScript Sandbox — effectively instrument JavaScript code in a way that is not observable</li>
</ul>
</div>
</div>
</div>
<divclass="section">
<divclass="content-container">
<h2>Related Technologies</h2>
<divclass="align-left">
<p>
The <ahref="https://developers.google.com/closure/compiler/">Closure Compiler</a> also optimizes JavaScript code. Prepack goes further by truly running the global code that initialization phase, unrolling loops and recursion. Prepack focuses on runtime performance, while the Closure Compiler emphasizes JavaScript code size.
You can follow the <ahref="https://github.com/facebook/prepack/blob/master/CONTRIBUTING.md">contributing guidelines</a>. There is also a <ahref="https://github.com/facebook/prepack/wiki/Suggested-reading">suggested reading list </a> to learn more about the internals of the project.