node/test/parallel/test-compile-cache-typescript-strip-sourcemaps.js
Joyee Cheung 4a5d2c7538
Some checks are pending
Coverage Linux (without intl) / coverage-linux-without-intl (push) Waiting to run
Coverage Linux / coverage-linux (push) Waiting to run
Coverage Windows / coverage-windows (push) Waiting to run
Test and upload documentation to artifacts / build-docs (push) Waiting to run
Linters / lint-addon-docs (push) Waiting to run
Linters / lint-cpp (push) Waiting to run
Linters / format-cpp (push) Waiting to run
Linters / lint-js-and-md (push) Waiting to run
Linters / lint-py (push) Waiting to run
Linters / lint-yaml (push) Waiting to run
Linters / lint-sh (push) Waiting to run
Linters / lint-codeowners (push) Waiting to run
Linters / lint-pr-url (push) Waiting to run
Linters / lint-readme (push) Waiting to run
Notify on Push / Notify on Force Push on `main` (push) Waiting to run
Notify on Push / Notify on Push on `main` that lacks metadata (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
module: integrate TypeScript into compile cache
This integrates TypeScript into the compile cache by caching
the transpilation (either type-stripping or transforming) output
in addition to the V8 code cache that's generated from the
transpilation output.

Locally this speeds up loading with type stripping of
`benchmark/fixtures/strip-types-benchmark.ts` by ~65% and
loading with type transforms of
`fixtures/transform-types-benchmark.ts` by ~128%.

When comparing loading .ts and loading pre-transpiled .js on-disk
with the compile cache enabled, previously .ts loaded 46% slower
with type-stripping and 66% slower with transforms compared to
loading .js files directly.
After this patch, .ts loads 12% slower with type-stripping and
22% slower with transforms compared to .js.

(Note that the numbers are based on microbenchmark fixtures and
do not necessarily represent real-world workloads, though with
bigger real-world files, the speed up should be more significant).

PR-URL: https://github.com/nodejs/node/pull/56629
Fixes: https://github.com/nodejs/node/issues/54741
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2025-01-25 02:30:27 +00:00

60 lines
2.1 KiB
JavaScript

'use strict';
// This tests NODE_COMPILE_CACHE can be used for type stripping and ignores
// --enable-source-maps as there's no difference in the code generated.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
tmpdir.refresh();
const dir = tmpdir.resolve('.compile_cache_dir');
const script = fixtures.path('typescript', 'ts', 'test-typescript.ts');
spawnSyncAndAssert(
process.execPath,
[script],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: dir
},
cwd: tmpdir.path
},
{
stderr(output) {
assert.match(output, /saving transpilation cache for StrippedTypeScript .*test-typescript\.ts/);
assert.match(output, /writing cache for StrippedTypeScript .*test-typescript\.ts.*success/);
assert.match(output, /writing cache for CommonJS .*test-typescript\.ts.*success/);
return true;
}
});
// Reloading with source maps should hit the cache generated without source maps, because for
// type stripping, only sourceURL is added regardless of whether source map is enabled.
spawnSyncAndAssert(
process.execPath,
['--enable-source-maps', script],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: dir
},
cwd: tmpdir.path
},
{
stderr(output) {
// Both the transpile cache and the code cache should be missed.
assert.match(output, /retrieving transpile cache for StrippedTypeScript .*test-typescript\.ts.*success/);
assert.match(output, /reading cache from .* for CommonJS .*test-typescript\.ts.*success/);
assert.match(output, /skip persisting StrippedTypeScript .*test-typescript\.ts because cache was the same/);
assert.match(output, /V8 code cache for CommonJS .*test-typescript\.ts was accepted, keeping the in-memory entry/);
assert.match(output, /skip persisting CommonJS .*test-typescript\.ts because cache was the same/);
return true;
}
});