11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst { it } = require('node:test');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
61cb0ef41Sopenharmony_ci  n: [100, 1000, 1e4],
71cb0ef41Sopenharmony_ci  type: ['sync', 'async'],
81cb0ef41Sopenharmony_ci}, {
91cb0ef41Sopenharmony_ci  // We don't want to test the reporter here
101cb0ef41Sopenharmony_ci  flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
111cb0ef41Sopenharmony_ci});
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciasync function run(n, type) {
141cb0ef41Sopenharmony_ci  const promises = new Array(n);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-unused-vars
171cb0ef41Sopenharmony_ci  let avoidV8Optimization;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  switch (type) {
201cb0ef41Sopenharmony_ci    case 'sync': {
211cb0ef41Sopenharmony_ci      for (let i = 0; i < n; i++) {
221cb0ef41Sopenharmony_ci        promises[i] = it(`${i}`, () => {
231cb0ef41Sopenharmony_ci          avoidV8Optimization = i;
241cb0ef41Sopenharmony_ci        });
251cb0ef41Sopenharmony_ci      }
261cb0ef41Sopenharmony_ci      break;
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    case 'async':
301cb0ef41Sopenharmony_ci      for (let i = 0; i < n; i++) {
311cb0ef41Sopenharmony_ci        promises[i] = it(`${i}`, async () => {
321cb0ef41Sopenharmony_ci          avoidV8Optimization = i;
331cb0ef41Sopenharmony_ci        });
341cb0ef41Sopenharmony_ci      }
351cb0ef41Sopenharmony_ci      break;
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  await Promise.all(promises);
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cifunction main({ n, type }) {
421cb0ef41Sopenharmony_ci  bench.start();
431cb0ef41Sopenharmony_ci  run(n, type).then(() => {
441cb0ef41Sopenharmony_ci    bench.end(n);
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci}
47