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: ['copy', 'rest', 'arguments'], 81cb0ef41Sopenharmony_ci n: [1e8], 91cb0ef41Sopenharmony_ci}); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cifunction copyArguments() { 121cb0ef41Sopenharmony_ci const len = arguments.length; 131cb0ef41Sopenharmony_ci const args = new Array(len); 141cb0ef41Sopenharmony_ci for (let i = 0; i < len; i++) 151cb0ef41Sopenharmony_ci args[i] = arguments[i]; 161cb0ef41Sopenharmony_ci assert.strictEqual(args[0], 1); 171cb0ef41Sopenharmony_ci assert.strictEqual(args[1], 2); 181cb0ef41Sopenharmony_ci assert.strictEqual(args[2], 'a'); 191cb0ef41Sopenharmony_ci assert.strictEqual(args[3], 'b'); 201cb0ef41Sopenharmony_ci} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cifunction restArguments(...args) { 231cb0ef41Sopenharmony_ci assert.strictEqual(args[0], 1); 241cb0ef41Sopenharmony_ci assert.strictEqual(args[1], 2); 251cb0ef41Sopenharmony_ci assert.strictEqual(args[2], 'a'); 261cb0ef41Sopenharmony_ci assert.strictEqual(args[3], 'b'); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cifunction useArguments() { 301cb0ef41Sopenharmony_ci assert.strictEqual(arguments[0], 1); 311cb0ef41Sopenharmony_ci assert.strictEqual(arguments[1], 2); 321cb0ef41Sopenharmony_ci assert.strictEqual(arguments[2], 'a'); 331cb0ef41Sopenharmony_ci assert.strictEqual(arguments[3], 'b'); 341cb0ef41Sopenharmony_ci} 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cifunction runCopyArguments(n) { 371cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 381cb0ef41Sopenharmony_ci copyArguments(1, 2, 'a', 'b'); 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cifunction runRestArguments(n) { 421cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 431cb0ef41Sopenharmony_ci restArguments(1, 2, 'a', 'b'); 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cifunction runUseArguments(n) { 471cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 481cb0ef41Sopenharmony_ci useArguments(1, 2, 'a', 'b'); 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_cifunction main({ n, method }) { 521cb0ef41Sopenharmony_ci let fn; 531cb0ef41Sopenharmony_ci switch (method) { 541cb0ef41Sopenharmony_ci case 'copy': 551cb0ef41Sopenharmony_ci fn = runCopyArguments; 561cb0ef41Sopenharmony_ci break; 571cb0ef41Sopenharmony_ci case 'rest': 581cb0ef41Sopenharmony_ci fn = runRestArguments; 591cb0ef41Sopenharmony_ci break; 601cb0ef41Sopenharmony_ci case 'arguments': 611cb0ef41Sopenharmony_ci fn = runUseArguments; 621cb0ef41Sopenharmony_ci break; 631cb0ef41Sopenharmony_ci default: 641cb0ef41Sopenharmony_ci throw new Error(`Unexpected method "${method}"`); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci bench.start(); 671cb0ef41Sopenharmony_ci fn(n); 681cb0ef41Sopenharmony_ci bench.end(n); 691cb0ef41Sopenharmony_ci} 70