1'use strict'; 2 3const assert = require('assert'); 4const common = require('../common.js'); 5 6const configs = { 7 n: [1e3], 8 mode: ['Array', 'repeat'], 9 encoding: ['ascii', 'utf8'], 10 size: [1e1, 1e3, 1e6], 11}; 12 13const bench = common.createBenchmark(main, configs); 14 15function main({ n, size, encoding, mode }) { 16 const character = encoding === 'ascii' ? 'a' : '\ud83d\udc0e'; // '' 17 18 let str; 19 20 switch (mode) { 21 case 'Array': 22 bench.start(); 23 for (let i = 0; i < n; i++) 24 str = new Array(size + 1).join(character); 25 bench.end(n); 26 break; 27 case 'repeat': 28 bench.start(); 29 for (let i = 0; i < n; i++) 30 str = character.repeat(size); 31 bench.end(n); 32 break; 33 default: 34 throw new Error(`Unexpected method "${mode}"`); 35 } 36 37 assert.strictEqual([...str].length, size); 38} 39