• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const fs = require('fs');
3const path = require('path');
4const common = require('../common.js');
5const { strictEqual } = require('assert');
6
7const tmpdir = require('../../test/common/tmpdir');
8const benchmarkDirectory =
9  path.resolve(tmpdir.path, 'benchmark-esm-parse');
10
11const bench = common.createBenchmark(main, {
12  n: [1e2]
13});
14
15async function main({ n }) {
16  tmpdir.refresh();
17
18  fs.mkdirSync(benchmarkDirectory);
19
20  let sampleSource = 'try {\n';
21  for (let i = 0; i < 1000; i++) {
22    sampleSource += 'sample.js(() => file = /test/);\n';
23  }
24  sampleSource += '} catch {}\nexports.p = 5;\n';
25
26  for (let i = 0; i < n; i++) {
27    const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
28    fs.writeFileSync(sampleFile, sampleSource);
29  }
30
31  bench.start();
32  for (let i = 0; i < n; i++) {
33    const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
34    const m = await import('file:' + sampleFile);
35    strictEqual(m.p, 5);
36  }
37  bench.end(n);
38
39  tmpdir.refresh();
40}
41