0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-22 07:37:56 +01:00
nodejs/benchmark/esm/import-meta.js
Joyee Cheung 4e23d6904c
benchmark: rewrite import.meta benchmark
This is a ESM benchmark, rewrite it so that we are directly
benchmarking the ESM import.meta paths and using number of
loads for op/s calculation, instead of doing it in startup
benchmarks and nesting number of process/workers spawn
for op/s calculation.

PR-URL: https://github.com/nodejs/node/pull/50683
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
2023-11-18 00:46:42 +00:00

33 lines
739 B
JavaScript

'use strict';
const path = require('path');
const { pathToFileURL, fileURLToPath } = require('url');
const common = require('../common');
const assert = require('assert');
const bench = common.createBenchmark(main, {
n: [1000],
});
const file = pathToFileURL(
path.resolve(__filename, '../../fixtures/esm-dir-file.mjs'),
);
async function load(array, n) {
for (let i = 0; i < n; i++) {
array[i] = await import(`${file}?i=${i}`);
}
return array;
}
function main({ n }) {
const array = [];
for (let i = 0; i < n; ++i) {
array.push({ dirname: '', filename: '', i: 0 });
}
bench.start();
load(array, n).then((arr) => {
bench.end(n);
assert.strictEqual(arr[n - 1].filename, fileURLToPath(file));
});
}