Speed Up Node.js Startup: Enabling V8 Code Cache in Production
Enable V8’s code‑caching in Node.js, cut startup latency, and verify cache usage with step‑by‑step commands and practical recovery options.
07 Jan 2026, 07:48 UTC

Desired Outcome
Reduce the time it takes a Node.js application to start by persisting V8‑generated code caches across runs. Subsequent launches skip the expensive parsing and baseline compilation stages, leading to measurable startup latency savings.
Prerequisites
- Node.js 12.0.0 or newer (bundles V8 7.4+). Check your version with
node -v. - A writable directory for cache files (e.g.,
./v8-code-cache). The directory must be accessible to the user that runs Node. - Permission to launch Node with custom V8 flags. This can be done via a wrapper script,
npmscripts, or directly from the command line.
Procedure
- Create the cache directory
mkdir -p ./v8-code-cache chmod 755 ./v8-code-cacheReplace
./v8-code-cachewith a path that fits your deployment layout. - Run the application with the code‑cache flag
node --code-cache-dir=./v8-code-cache your-app.jsDuring this first run, V8 will generate cache files named
*.js.code-cachefor each script it compiles. - Verify cache creation
ls -la ./v8-code-cacheYou should see one or more files matching
*.js.code-cache. Example output:drwxr-xr-x 2 user user 4096 Oct 10 23:20 . -rw-r--r-- 1 user user 1234 Oct 10 23:20 index.js.code-cache - Measure startup time before and after enabling the flag
# Without cache /usr/bin/time -f "Elapsed: %e" node your-app.js # With cache /usr/bin/time -f "Elapsed: %e" node --code-cache-dir=./v8-code-cache your-app.jsOn a typical workload you should observe a reduction of 10–30% in elapsed time, depending on script size and complexity.
- Optional: Enable verbose logging to confirm cache hits
node --code-cache-dir=./v8-code-cache --trace-code-cache your-app.jsLook for log lines such as
code cache hitorcode cache miss. A low number of misses indicates the cache is being used.
Expected Checks
- Cache files exist after the first run.
- Subsequent launches show reduced startup time.
- V8 logs (if
--trace-code-cacheis enabled) show cache hits and few misses. - No V8 warnings about corrupted caches appear.
Recovery Options
- If the application crashes or behaves incorrectly after caching is enabled, delete the cache directory:
rm -rf ./v8-code-cache. V8 will rebuild fresh caches on the next run. - Point
--code-cache-dirto a new directory if you suspect corruption or permission issues. - Clear the cache when deploying a new version of the application. V8 automatically detects source changes and falls back to interpretation, but a clean cache avoids stale data.
Caveats & Best Practices
- Code caching benefits scripts that are executed repeatedly. One‑off scripts may see negligible improvement and incur extra disk I/O.
- Short‑lived worker processes or microservices that start and stop often may not benefit.
- Always version your cache directory (e.g.,
./v8-code-cache-v1) or clear it on deployment to avoid stale caches after code changes. - Keep the cache directory on a fast storage medium (SSD) to avoid I/O bottlenecks.
- Monitor startup times in production to confirm ongoing benefits.
Flag Summary
| Flag | Description |
|---|---|
--code-cache-dir=DIR | Specifies the directory where V8 stores and reads code cache files. |
--trace-code-cache | Enables logging of cache hits/misses for debugging. |
Practical Example: Using a Wrapper Script
To simplify usage, create start.js:
#!/usr/bin/env node
const { spawn } = require('child_process');
const path = require('path');
const cacheDir = path.resolve(__dirname, 'v8-code-cache');
const args = ['--code-cache-dir=' + cacheDir, 'app.js'];
const node = spawn('node', args, { stdio: 'inherit' });
node.on('exit', code => process.exit(code));
Make it executable (chmod +x start.js) and run with ./start.js. This encapsulates the flag usage and keeps your npm start command clean.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.