mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
benchmark: disambiguate filename
and dirname
read perf
PR-URL: https://github.com/nodejs/node/pull/58056 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
parent
397802e9f6
commit
99c2c900d5
4 changed files with 16 additions and 9 deletions
|
@ -1,32 +1,37 @@
|
|||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const { pathToFileURL, fileURLToPath } = require('url');
|
||||
const { pathToFileURL } = require('url');
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [1000],
|
||||
valuesToRead: [
|
||||
'dirname-and-filename',
|
||||
'dirname',
|
||||
'filename',
|
||||
],
|
||||
});
|
||||
|
||||
const file = pathToFileURL(
|
||||
path.resolve(__filename, '../../fixtures/esm-dir-file.mjs'),
|
||||
);
|
||||
async function load(array, n) {
|
||||
const fixtureDir = path.resolve(__filename, '../../fixtures');
|
||||
const fixtureDirURL = pathToFileURL(fixtureDir);
|
||||
async function load(array, n, valuesToRead) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
array[i] = await import(`${file}?i=${i}`);
|
||||
array[i] = await import(`${fixtureDirURL}/import-meta-${valuesToRead}.mjs?i=${i}`);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
function main({ n }) {
|
||||
function main({ n, valuesToRead }) {
|
||||
const array = [];
|
||||
for (let i = 0; i < n; ++i) {
|
||||
array.push({ dirname: '', filename: '', i: 0 });
|
||||
}
|
||||
|
||||
bench.start();
|
||||
load(array, n).then((arr) => {
|
||||
load(array, n, valuesToRead).then((arr) => {
|
||||
bench.end(n);
|
||||
assert.strictEqual(arr[n - 1].filename, fileURLToPath(file));
|
||||
if (valuesToRead.includes('dirname')) assert.strictEqual(arr[n - 1].dirname, fixtureDir);
|
||||
if (valuesToRead.includes('filename')) assert.strictEqual(arr[n - 1].filename, path.join(fixtureDir, `import-meta-${valuesToRead}.mjs`));
|
||||
});
|
||||
}
|
||||
|
|
1
benchmark/fixtures/import-meta-dirname.mjs
Normal file
1
benchmark/fixtures/import-meta-dirname.mjs
Normal file
|
@ -0,0 +1 @@
|
|||
export const dirname = import.meta.dirname;
|
1
benchmark/fixtures/import-meta-filename.mjs
Normal file
1
benchmark/fixtures/import-meta-filename.mjs
Normal file
|
@ -0,0 +1 @@
|
|||
export const filename = import.meta.filename;
|
Loading…
Add table
Add a link
Reference in a new issue