11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 71cb0ef41Sopenharmony_ci method: ['apply', 'spread', 'call-spread'], 81cb0ef41Sopenharmony_ci count: [5, 10, 20], 91cb0ef41Sopenharmony_ci context: ['context', 'null'], 101cb0ef41Sopenharmony_ci rest: [0, 1], 111cb0ef41Sopenharmony_ci n: [5e6], 121cb0ef41Sopenharmony_ci}); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cifunction makeTest(count, rest) { 151cb0ef41Sopenharmony_ci if (rest) { 161cb0ef41Sopenharmony_ci return function test(...args) { 171cb0ef41Sopenharmony_ci assert.strictEqual(count, args.length); 181cb0ef41Sopenharmony_ci }; 191cb0ef41Sopenharmony_ci } 201cb0ef41Sopenharmony_ci return function test() { 211cb0ef41Sopenharmony_ci assert.strictEqual(count, arguments.length); 221cb0ef41Sopenharmony_ci }; 231cb0ef41Sopenharmony_ci} 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_cifunction main({ n, context, count, rest, method }) { 261cb0ef41Sopenharmony_ci const ctx = context === 'context' ? {} : null; 271cb0ef41Sopenharmony_ci let fn = makeTest(count, rest); 281cb0ef41Sopenharmony_ci const args = new Array(count); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci for (let i = 0; i < count; i++) 311cb0ef41Sopenharmony_ci args[i] = i; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci switch (method) { 341cb0ef41Sopenharmony_ci case 'apply': 351cb0ef41Sopenharmony_ci bench.start(); 361cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 371cb0ef41Sopenharmony_ci fn.apply(ctx, args); 381cb0ef41Sopenharmony_ci bench.end(n); 391cb0ef41Sopenharmony_ci break; 401cb0ef41Sopenharmony_ci case 'spread': 411cb0ef41Sopenharmony_ci if (ctx !== null) 421cb0ef41Sopenharmony_ci fn = fn.bind(ctx); 431cb0ef41Sopenharmony_ci bench.start(); 441cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 451cb0ef41Sopenharmony_ci fn(...args); 461cb0ef41Sopenharmony_ci bench.end(n); 471cb0ef41Sopenharmony_ci break; 481cb0ef41Sopenharmony_ci case 'call-spread': 491cb0ef41Sopenharmony_ci bench.start(); 501cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 511cb0ef41Sopenharmony_ci fn.call(ctx, ...args); 521cb0ef41Sopenharmony_ci bench.end(n); 531cb0ef41Sopenharmony_ci break; 541cb0ef41Sopenharmony_ci default: 551cb0ef41Sopenharmony_ci throw new Error(`Unexpected method "${method}"`); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci} 58