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: ['withoutdefaults', 'withdefaults'], 81cb0ef41Sopenharmony_ci n: [1e8], 91cb0ef41Sopenharmony_ci}); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cifunction oldStyleDefaults(x, y) { 121cb0ef41Sopenharmony_ci x = x || 1; 131cb0ef41Sopenharmony_ci y = y || 2; 141cb0ef41Sopenharmony_ci assert.strictEqual(x, 1); 151cb0ef41Sopenharmony_ci assert.strictEqual(y, 2); 161cb0ef41Sopenharmony_ci} 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction defaultParams(x = 1, y = 2) { 191cb0ef41Sopenharmony_ci assert.strictEqual(x, 1); 201cb0ef41Sopenharmony_ci assert.strictEqual(y, 2); 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cifunction runOldStyleDefaults(n) { 241cb0ef41Sopenharmony_ci bench.start(); 251cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 261cb0ef41Sopenharmony_ci oldStyleDefaults(); 271cb0ef41Sopenharmony_ci bench.end(n); 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_cifunction runDefaultParams(n) { 311cb0ef41Sopenharmony_ci bench.start(); 321cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) 331cb0ef41Sopenharmony_ci defaultParams(); 341cb0ef41Sopenharmony_ci bench.end(n); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_cifunction main({ n, method }) { 381cb0ef41Sopenharmony_ci switch (method) { 391cb0ef41Sopenharmony_ci case 'withoutdefaults': 401cb0ef41Sopenharmony_ci runOldStyleDefaults(n); 411cb0ef41Sopenharmony_ci break; 421cb0ef41Sopenharmony_ci case 'withdefaults': 431cb0ef41Sopenharmony_ci runDefaultParams(n); 441cb0ef41Sopenharmony_ci break; 451cb0ef41Sopenharmony_ci default: 461cb0ef41Sopenharmony_ci throw new Error(`Unexpected method "${method}"`); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci} 49