Skip to content

Commit

Permalink
benchmark: rewrite import.meta benchmark
Browse files Browse the repository at this point in the history
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: #50683
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
  • Loading branch information
joyeecheung authored and RafaelGSS committed Nov 29, 2023
1 parent aebe2fc commit 23f56d8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
32 changes: 32 additions & 0 deletions benchmark/esm/import-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'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));
});
}
5 changes: 2 additions & 3 deletions benchmark/fixtures/esm-dir-file.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import assert from 'assert';
assert.ok(import.meta.dirname);
assert.ok(import.meta.filename);
export const dirname = import.meta.dirname;
export const filename = import.meta.filename;
5 changes: 0 additions & 5 deletions benchmark/fixtures/load-esm-dir-file.js

This file was deleted.

1 change: 0 additions & 1 deletion benchmark/misc/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const bench = common.createBenchmark(main, {
script: [
'benchmark/fixtures/require-builtins',
'test/fixtures/semicolon',
'benchmark/fixtures/load-esm-dir-file',
],
mode: ['process', 'worker'],
count: [30],
Expand Down

0 comments on commit 23f56d8

Please sign in to comment.