- Node.js Startup Snapshots enhance startup performance by preloading essential features.
- The integration of Startup Snapshots addresses the challenge of balancing new features with startup speed.
- Node.js employs lazy loading, precompiled code cache, and Startup Snapshots for efficient startup.
- Startup Snapshots serialize V8 heap for faster initialization and support userland application snapshots.
- Custom snapshots can improve startup times for applications like command-line tools.
Node.js has been evolving rapidly, adding numerous features and modules to its core. This growth presents challenges, particularly in maintaining efficient startup performance. To address this, Node.js has introduced the Startup Snapshot feature, which significantly enhances startup speed for both the core and userland applications.
The journey towards Startup Snapshots began as Node.js shifted from its small core philosophy towards a more feature-rich environment. This transition involved adding various new globals, web APIs, built-in modules, and APIs, all of which required additional setup during startup. The challenge was to incorporate these features without compromising startup performance.
Node.js is implemented in both JavaScript and C++, with a significant portion of the internals written in JavaScript. This approach lowers the contribution barrier and reduces callback costs between C++ and JavaScript. However, it also poses a challenge: JavaScript code needs to be parsed and compiled before execution, which can slow down startup times. Furthermore, initialization code runs only once at startup, preventing optimization by the JavaScript engine.
To mitigate these issues, Node.js employs several strategies. First, it avoids initializing all globals and built-ins at startup. Experimental or less commonly used features are loaded lazily upon first access. Second, during release, Node.js precompiles internal modules to create a code cache, which is then embedded in the executable. This allows V8 to skip parsing and compilation when loading additional modules, using the precompiled bytecode and metadata instead.
For essential features that are frequently used, Node.js captures them in a V8 Startup Snapshot. This snapshot helps skip the execution of initialization code, saving time during startup. Startup Snapshots serialize the V8 heap into a binary blob, capturing both primitives and execution contexts. Node.js uses isolate snapshots for all isolates, including main and worker isolates. Built-in context snapshots are available for main, VM, and worker contexts, although the worker context snapshot is minimal.
The benefits of Startup Snapshots are evident in startup performance improvements. For instance, on a MacBook, Node.js startup time can be reduced from 40 milliseconds to 20 milliseconds when using a snapshot. This increase in speed simplifies the flame graph and reduces the workload during startup.
Startup Snapshots also provide sustainability as Node.js continues to grow. By keeping the startup process efficient, Node.js can introduce new features without sacrificing performance. Moreover, the feature is now available to users, allowing them to create snapshots of their own applications. This is particularly useful for applications where startup performance is critical, such as command-line tools.
Creating a custom snapshot involves a workflow similar to building the core snapshot. Users can provide a script for essential initialization, and Node.js can run the script to completion. After all asynchronous operations are resolved, Node.js takes a snapshot of the heap and writes it to a binary blob. This blob can be embedded in the Node.js executable or stored separately on disk.
Currently, the userland snapshot process requires bundling setup code into a single file. However, support for module-based snapshots is in development. There are two main methods to generate a userland snapshot: building Node.js from source with the custom snapshot option or using the official Node.js executable with runtime options for snapshot generation.
A work-in-progress feature, the single executable application, aims to streamline the snapshot process. This feature will allow users to generate and inject a snapshot into a single executable without compiling Node.js from source. This development promises to make snapshot creation more accessible and efficient.
Node.js provides several JavaScript APIs to help synchronize runtime states in the snapshot script. By default, Node.js refreshes runtime states like environment variables during deserialization. Users can use APIs to synchronize or defer computations based on these states, ensuring accurate runtime behavior.
In summary, the integration of Startup Snapshots into Node.js core has significantly improved startup performance. The feature is now available for userland applications, offering JavaScript APIs to assist in building custom snapshots. Ongoing developments, including the single executable application, will further enhance the ease and efficacy of using Startup Snapshots in Node.js applications.