Skip to content

Commit 275215f

Browse files
joyeecheungnodejs-github-bot
authored andcommitted
benchmark: use typescript for import cjs benchmark
The original benchmark uses a not very realistic fixture (it has a huge try-catch block that would throw on the first line and then export at the end, hardly representative of real-world code). Also, it measures the entire import including evaluation, not just parsing. This updates the name to import-cjs to be more accurate, and use the typescript.js as the fixture which has been reported to be slow to import, leading users to use require() to work around the peformance impact. It splits the measurement into two different types: parsing CJS for the first time (where the overhead of loading the lexer makes a difference) and parsing CJS after the lexer has been loaded. PR-URL: #60663 Refs: #59913 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 2388991 commit 275215f

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

benchmark/esm/cjs-parse.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

benchmark/esm/import-cjs.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('assert');
4+
const tmpdir = require('../../test/common/tmpdir');
5+
const fixtures = require('../../test/common/fixtures.js');
6+
const fs = require('fs');
7+
8+
// This fixture has 2000+ exports.
9+
const fixtureFile = fixtures.path('snapshot', 'typescript.js');
10+
// This is fixed - typescript.js is slow to load, so 10 is enough.
11+
// And we want to measure either 10 warm loads, or 1 cold load (but compare.js would run it many times)
12+
const runs = 10;
13+
const bench = common.createBenchmark(main, {
14+
type: ['cold', 'warm'],
15+
}, {
16+
setup() {
17+
tmpdir.refresh();
18+
fs.cpSync(fixtureFile, tmpdir.resolve(`imported-cjs-initial.js`));
19+
for (let i = 0; i < runs; i++) {
20+
fs.cpSync(fixtureFile, tmpdir.resolve(`imported-cjs-${i}.js`));
21+
}
22+
},
23+
});
24+
25+
async function main({ type }) {
26+
if (type === 'cold') {
27+
bench.start();
28+
await import(tmpdir.fileURL(`imported-cjs-initial.js`));
29+
bench.end(1);
30+
} else {
31+
await import(tmpdir.fileURL(`imported-cjs-initial.js`)); // Initialize the wasm first.
32+
bench.start();
33+
let result;
34+
for (let i = 0; i < runs; i++) {
35+
result = await import(tmpdir.fileURL(`imported-cjs-${i}.js`));
36+
}
37+
bench.end(runs);
38+
const mod = require(fixtures.path('snapshot', 'typescript.js'));
39+
assert.deepStrictEqual(Object.keys(mod), Object.keys(result.default));
40+
}
41+
}

0 commit comments

Comments
 (0)