Skip to content

Commit ecc107d

Browse files
committed
perf(@angular/cli): enable Node.js compile code cache when available
The Angular CLI will now enable the Node.js compile cache when available for use. Node.js v22.8 and higher currently provide support for this feature. The compile cache stores the v8 intermediate forms of JavaScript code for the Angular CLI itself. This provides a speed up to initialization on subsequent uses the Angular CLI. The Node.js cache is stored in a temporary directory in a globally accessible location so that all Node.js instances of a compatible version can share the cache. The code cache can be disabled if preferred via `NODE_DISABLE_COMPILE_CACHE=1`. Based on initial profiling, this change provides an ~6% production build time improvement for a newly generated project once the cache is available. ``` Benchmark 1: NODE_DISABLE_COMPILE_CACHE=1 node ./node_modules/.bin/ng build Time (mean ± σ): 2.617 s ± 0.016 s [User: 3.795 s, System: 1.284 s] Range (min … max): 2.597 s … 2.640 s 10 runs Benchmark 2: node ./node_modules/.bin/ng build Time (mean ± σ): 2.475 s ± 0.017 s [User: 3.555 s, System: 1.354 s] Range (min … max): 2.454 s … 2.510 s 10 runs Summary node ./node_modules/.bin/ng build ran 1.06 ± 0.01 times faster than NODE_DISABLE_COMPILE_CACHE=1 node ./node_modules/.bin/ng build ```
1 parent 84e14d6 commit ecc107d

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import type { CompilerOptions } from '@angular/compiler-cli';
1010
import type { PartialMessage } from 'esbuild';
11-
import { createRequire } from 'node:module';
11+
import { createRequire, getCompileCacheDir } from 'node:module';
1212
import { MessageChannel } from 'node:worker_threads';
1313
import Piscina from 'piscina';
1414
import type { SourceFile } from 'typescript';
@@ -41,6 +41,11 @@ export class ParallelCompilation extends AngularCompilation {
4141
useAtomics: !process.versions.webcontainer,
4242
filename: localRequire.resolve('./parallel-worker'),
4343
recordTiming: false,
44+
env: {
45+
...process.env,
46+
// Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+)
47+
'NODE_COMPILE_CACHE': getCompileCacheDir?.(),
48+
},
4449
});
4550
}
4651

packages/angular/build/src/tools/esbuild/javascript-transformer.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { createHash } from 'node:crypto';
1010
import { readFile } from 'node:fs/promises';
11+
import { getCompileCacheDir } from 'node:module';
1112
import Piscina from 'piscina';
1213
import { Cache } from './cache';
1314

@@ -62,6 +63,11 @@ export class JavaScriptTransformer {
6263
// Shutdown idle threads after 1 second of inactivity
6364
idleTimeout: 1000,
6465
recordTiming: false,
66+
env: {
67+
...process.env,
68+
// Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+)
69+
'NODE_COMPILE_CACHE': getCompileCacheDir?.(),
70+
},
6571
});
6672

6773
return this.#workerPool;

packages/angular/build/src/tools/sass/sass-service.ts

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import assert from 'node:assert';
10+
import { getCompileCacheDir } from 'node:module';
1011
import { fileURLToPath, pathToFileURL } from 'node:url';
1112
import { MessageChannel } from 'node:worker_threads';
1213
import { Piscina } from 'piscina';
@@ -101,6 +102,11 @@ export class SassWorkerImplementation {
101102
// Shutdown idle threads after 1 second of inactivity
102103
idleTimeout: 1000,
103104
recordTiming: false,
105+
env: {
106+
...process.env,
107+
// Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+)
108+
'NODE_COMPILE_CACHE': getCompileCacheDir?.(),
109+
},
104110
});
105111

106112
return this.#workerPool;

packages/angular/build/src/typings.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@
1717
declare module 'esbuild' {
1818
export * from 'esbuild-wasm';
1919
}
20+
21+
/**
22+
* Augment the Node.js module builtin types to support the v22.8+ compile cache functions
23+
*/
24+
declare module 'node:module' {
25+
function getCompileCacheDir(): string | undefined;
26+
}

packages/angular/cli/bin/bootstrap.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@
1818
* range.
1919
*/
2020

21-
import('../lib/init.js');
21+
// Enable on-disk code caching if available (Node.js 22.8+)
22+
try {
23+
const { enableCompileCache } = require('node:module');
24+
25+
enableCompileCache?.();
26+
} catch {}
27+
28+
// Initialize the Angular CLI
29+
void import('../lib/init.js');

0 commit comments

Comments
 (0)