0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/es/string-repeat.js
Ethan Arrowood af49e58211 test: add benchmark tests for es
Added parallel test benchmark for es

PR-URL: https://github.com/nodejs/node/pull/16076
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-10-12 17:41:30 -07:00

43 lines
952 B
JavaScript

'use strict';
const assert = require('assert');
const common = require('../common.js');
const configs = {
n: [1e3],
mode: ['Array', 'repeat'],
encoding: ['ascii', 'utf8'],
size: [1e1, 1e3, 1e6],
};
const bench = common.createBenchmark(main, configs);
function main(conf) {
const n = +conf.n;
const size = +conf.size;
const character = conf.encoding === 'ascii' ? 'a' : '\ud83d\udc0e'; // '🐎'
let str;
switch (conf.mode) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'Array':
bench.start();
for (let i = 0; i < n; i++)
str = new Array(size + 1).join(character);
bench.end(n);
break;
case 'repeat':
bench.start();
for (let i = 0; i < n; i++)
str = character.repeat(size);
bench.end(n);
break;
default:
throw new Error('Unexpected method');
}
assert.strictEqual([...str].length, size);
}