Skip to content

Commit 00d5d09

Browse files
committed
perf(@angular/build): optimize template string size calculation in server manifest
Calculates normalized byte lengths for server assets directly using Node.js Buffer.byteLength to avoid script compilation with vm.runInThisContext.
1 parent 3c7ac15 commit 00d5d09

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

  • packages/angular/build/src/utils/server-rendering

packages/angular/build/src/utils/server-rendering/manifest.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import type { Metafile } from 'esbuild';
1010
import { extname } from 'node:path';
11-
import { runInThisContext } from 'node:vm';
1211
import { NormalizedApplicationBuildOptions } from '../../builders/application/options';
1312
import {
1413
type BuildOutputFile,
@@ -174,9 +173,9 @@ export function generateAngularServerAppManifest(
174173
),
175174
);
176175

177-
// This is needed because JavaScript engines script parser convert `\r\n` to `\n` in template literals,
178-
// which can result in an incorrect byte length.
179-
const size = runInThisContext(`new TextEncoder().encode(\`${escapedContent}\`).byteLength`);
176+
// JavaScript engine script parsers normalize `\r\n` (2 bytes in UTF-8) to `\n` (1 byte in UTF-8) in template literals.
177+
// Subtracting the count of `\r\n` occurrences avoids allocating and copying a new string for large assets.
178+
const size = Buffer.byteLength(file.text) - (file.text.match(/\r\n/g)?.length ?? 0);
180179

181180
serverAssets[file.path] =
182181
`{size: ${size}, hash: '${file.hash}', text: () => import('./${jsChunkFilePath}').then(m => m.default)}`;

0 commit comments

Comments
 (0)