mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
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: https://github.com/nodejs/node/pull/60663 Refs: https://github.com/nodejs/node/issues/59913 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
'use strict';
|
|
const common = require('../common.js');
|
|
const assert = require('assert');
|
|
const tmpdir = require('../../test/common/tmpdir');
|
|
const fixtures = require('../../test/common/fixtures.js');
|
|
const fs = require('fs');
|
|
|
|
// This fixture has 2000+ exports.
|
|
const fixtureFile = fixtures.path('snapshot', 'typescript.js');
|
|
// This is fixed - typescript.js is slow to load, so 10 is enough.
|
|
// And we want to measure either 10 warm loads, or 1 cold load (but compare.js would run it many times)
|
|
const runs = 10;
|
|
const bench = common.createBenchmark(main, {
|
|
type: ['cold', 'warm'],
|
|
}, {
|
|
setup() {
|
|
tmpdir.refresh();
|
|
fs.cpSync(fixtureFile, tmpdir.resolve(`imported-cjs-initial.js`));
|
|
for (let i = 0; i < runs; i++) {
|
|
fs.cpSync(fixtureFile, tmpdir.resolve(`imported-cjs-${i}.js`));
|
|
}
|
|
},
|
|
});
|
|
|
|
async function main({ type }) {
|
|
if (type === 'cold') {
|
|
bench.start();
|
|
await import(tmpdir.fileURL(`imported-cjs-initial.js`));
|
|
bench.end(1);
|
|
} else {
|
|
await import(tmpdir.fileURL(`imported-cjs-initial.js`)); // Initialize the wasm first.
|
|
bench.start();
|
|
let result;
|
|
for (let i = 0; i < runs; i++) {
|
|
result = await import(tmpdir.fileURL(`imported-cjs-${i}.js`));
|
|
}
|
|
bench.end(runs);
|
|
const mod = require(fixtures.path('snapshot', 'typescript.js'));
|
|
assert.deepStrictEqual(Object.keys(mod), Object.keys(result.default));
|
|
}
|
|
}
|