V8 allows a creation of a heap snapshots. By using the mksnapshot tool provided by the V8 team you can create your own custom snapshots.
The atom team has published their work in which they used V8 snapshots to speed up the initial load time of the editor.
The following packages are needed:
|mksnapshot||Create the snapshot.|
There are 3 steps in creating a snapshot.
The first problem you encounter when creating a snapshot is that the snapshot itself is created in an “empty” V8 runtime.
This means in the context the snapshot is created there are no globals / object that you otherwise might have Node.js or in the browser. So there is no module object or window object.
In order to solve this the atom team have create the electron-link package. This package parses the source with babel (AST) and “replaces” the global objects that can be later or replaced.
According to this github comment this step is not needed anymore but i could not get it to work without it.
The output of this process is a generated js file that creates an object called snapshotResult that exposes the setGlobals and customRequire methods.
The creation of the snapshot is pretty trivial. All you have to do is to call the mksnapshot binary with the linked js file and set an output.
One thing to note that your packages electron version decides which binary version will be downloaded.
Another important aspect that the blobs name must be snapshot_blob.bin.
According to this forum post embedders can enable V8 to use snapshot_blob.bin and natives_blob.bin to “load” in snapshot and other native code.
By default there is a snapshot_blob.bin in your node_modules/electron/dist folder.
So you cannot require or import the blob, you have to overwrite the one electron is using. This is the way you can load in things to V8.
This is pseudo implementation of the process.
npm install electron electron-link mksnapshot
First we need to create the snapshot with npm run snapshot than use it with npm run start.
The file we want to create a snapshot of.
Create the snapshot and move it to the correct location.
Use in the created snapshot.
This should give you a basic idea about the process.