11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { createBenchmark } = require('../common.js');
41cb0ef41Sopenharmony_ciconst { format } = require('util');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst methods = [
71cb0ef41Sopenharmony_ci  'restAndSpread',
81cb0ef41Sopenharmony_ci  'argumentsAndApply',
91cb0ef41Sopenharmony_ci  'restAndApply',
101cb0ef41Sopenharmony_ci  'predefined',
111cb0ef41Sopenharmony_ci];
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst bench = createBenchmark(main, {
141cb0ef41Sopenharmony_ci  method: methods,
151cb0ef41Sopenharmony_ci  n: [1e6],
161cb0ef41Sopenharmony_ci});
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction usingRestAndSpread(...args) {
191cb0ef41Sopenharmony_ci  format(...args);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cifunction usingRestAndApply(...args) {
231cb0ef41Sopenharmony_ci  format.apply(null, args);
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cifunction usingArgumentsAndApply() {
271cb0ef41Sopenharmony_ci  format.apply(null, arguments);
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction usingPredefined() {
311cb0ef41Sopenharmony_ci  format('part 1', 'part', 2, 'part 3', 'part', 4);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cifunction main({ n, method, args }) {
351cb0ef41Sopenharmony_ci  let fn;
361cb0ef41Sopenharmony_ci  switch (method) {
371cb0ef41Sopenharmony_ci    case 'restAndSpread':
381cb0ef41Sopenharmony_ci      fn = usingRestAndSpread;
391cb0ef41Sopenharmony_ci      break;
401cb0ef41Sopenharmony_ci    case 'restAndApply':
411cb0ef41Sopenharmony_ci      fn = usingRestAndApply;
421cb0ef41Sopenharmony_ci      break;
431cb0ef41Sopenharmony_ci    case 'argumentsAndApply':
441cb0ef41Sopenharmony_ci      fn = usingArgumentsAndApply;
451cb0ef41Sopenharmony_ci      break;
461cb0ef41Sopenharmony_ci    case 'predefined':
471cb0ef41Sopenharmony_ci      fn = usingPredefined;
481cb0ef41Sopenharmony_ci      break;
491cb0ef41Sopenharmony_ci    default:
501cb0ef41Sopenharmony_ci      throw new Error(`Unexpected method "${method}"`);
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  bench.start();
541cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++)
551cb0ef41Sopenharmony_ci    fn('part 1', 'part', 2, 'part 3', 'part', 4);
561cb0ef41Sopenharmony_ci  bench.end(n);
571cb0ef41Sopenharmony_ci}
58