11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 61cb0ef41Sopenharmony_ci method: ['for', 'for-of', 'for-in', 'forEach'], 71cb0ef41Sopenharmony_ci count: [5, 10, 20, 100], 81cb0ef41Sopenharmony_ci n: [5e6], 91cb0ef41Sopenharmony_ci}); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cifunction useFor(n, items, count) { 121cb0ef41Sopenharmony_ci bench.start(); 131cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 141cb0ef41Sopenharmony_ci for (let j = 0; j < count; j++) { 151cb0ef41Sopenharmony_ci // eslint-disable-next-line no-unused-vars 161cb0ef41Sopenharmony_ci const item = items[j]; 171cb0ef41Sopenharmony_ci } 181cb0ef41Sopenharmony_ci } 191cb0ef41Sopenharmony_ci bench.end(n); 201cb0ef41Sopenharmony_ci} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cifunction useForOf(n, items) { 231cb0ef41Sopenharmony_ci bench.start(); 241cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 251cb0ef41Sopenharmony_ci // eslint-disable-next-line no-unused-vars, no-empty 261cb0ef41Sopenharmony_ci for (const item of items) {} 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci bench.end(n); 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cifunction useForIn(n, items) { 321cb0ef41Sopenharmony_ci bench.start(); 331cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 341cb0ef41Sopenharmony_ci for (const j in items) { 351cb0ef41Sopenharmony_ci // eslint-disable-next-line no-unused-vars 361cb0ef41Sopenharmony_ci const item = items[j]; 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci bench.end(n); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cifunction useForEach(n, items) { 431cb0ef41Sopenharmony_ci bench.start(); 441cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 451cb0ef41Sopenharmony_ci items.forEach((item) => {}); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci bench.end(n); 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cifunction main({ n, count, method }) { 511cb0ef41Sopenharmony_ci const items = new Array(count); 521cb0ef41Sopenharmony_ci let fn; 531cb0ef41Sopenharmony_ci for (let i = 0; i < count; i++) 541cb0ef41Sopenharmony_ci items[i] = i; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci switch (method) { 571cb0ef41Sopenharmony_ci case 'for': 581cb0ef41Sopenharmony_ci fn = useFor; 591cb0ef41Sopenharmony_ci break; 601cb0ef41Sopenharmony_ci case 'for-of': 611cb0ef41Sopenharmony_ci fn = useForOf; 621cb0ef41Sopenharmony_ci break; 631cb0ef41Sopenharmony_ci case 'for-in': 641cb0ef41Sopenharmony_ci fn = useForIn; 651cb0ef41Sopenharmony_ci break; 661cb0ef41Sopenharmony_ci case 'forEach': 671cb0ef41Sopenharmony_ci fn = useForEach; 681cb0ef41Sopenharmony_ci break; 691cb0ef41Sopenharmony_ci default: 701cb0ef41Sopenharmony_ci throw new Error(`Unexpected method "${method}"`); 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci fn(n, items, count); 731cb0ef41Sopenharmony_ci} 74