11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common.js'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 61cb0ef41Sopenharmony_ci loopMethod: ['forEach', 'iterator'], 71cb0ef41Sopenharmony_ci n: [1e6], 81cb0ef41Sopenharmony_ci}); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd'; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cifunction forEach(n) { 131cb0ef41Sopenharmony_ci const params = new URLSearchParams(str); 141cb0ef41Sopenharmony_ci const noDead = []; 151cb0ef41Sopenharmony_ci const cb = (val, key) => { 161cb0ef41Sopenharmony_ci noDead[0] = key; 171cb0ef41Sopenharmony_ci noDead[1] = val; 181cb0ef41Sopenharmony_ci }; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci bench.start(); 211cb0ef41Sopenharmony_ci for (let i = 0; i < n; i += 1) 221cb0ef41Sopenharmony_ci params.forEach(cb); 231cb0ef41Sopenharmony_ci bench.end(n); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci assert.strictEqual(noDead[0], 'three'); 261cb0ef41Sopenharmony_ci assert.strictEqual(noDead[1], '3rd'); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cifunction iterator(n) { 301cb0ef41Sopenharmony_ci const params = new URLSearchParams(str); 311cb0ef41Sopenharmony_ci const noDead = []; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci bench.start(); 341cb0ef41Sopenharmony_ci for (let i = 0; i < n; i += 1) { 351cb0ef41Sopenharmony_ci for (const pair of params) { 361cb0ef41Sopenharmony_ci noDead[0] = pair[0]; 371cb0ef41Sopenharmony_ci noDead[1] = pair[1]; 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci bench.end(n); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci assert.strictEqual(noDead[0], 'three'); 431cb0ef41Sopenharmony_ci assert.strictEqual(noDead[1], '3rd'); 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cifunction main({ loopMethod, n }) { 471cb0ef41Sopenharmony_ci switch (loopMethod) { 481cb0ef41Sopenharmony_ci case 'forEach': 491cb0ef41Sopenharmony_ci forEach(n); 501cb0ef41Sopenharmony_ci break; 511cb0ef41Sopenharmony_ci case 'iterator': 521cb0ef41Sopenharmony_ci iterator(n); 531cb0ef41Sopenharmony_ci break; 541cb0ef41Sopenharmony_ci default: 551cb0ef41Sopenharmony_ci throw new Error(`Unknown method ${loopMethod}`); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci} 58