benchmark: replace [].join() with ''.repeat()
Also add a benchmark to compare both ways to create strings. PR-URL: https://github.com/nodejs/node/pull/12170 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
9348f31c2a
commit
74dc3bfe08
@ -40,11 +40,11 @@ function main(conf) {
|
|||||||
var encoding;
|
var encoding;
|
||||||
switch (conf.type) {
|
switch (conf.type) {
|
||||||
case 'asc':
|
case 'asc':
|
||||||
message = new Array(conf.len + 1).join('a');
|
message = 'a'.repeat(conf.len);
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
message = new Array(conf.len / 2 + 1).join('ü');
|
message = 'ü'.repeat(conf.len / 2);
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
break;
|
break;
|
||||||
case 'buf':
|
case 'buf':
|
||||||
|
@ -25,11 +25,11 @@ function main(conf) {
|
|||||||
var encoding;
|
var encoding;
|
||||||
switch (conf.type) {
|
switch (conf.type) {
|
||||||
case 'asc':
|
case 'asc':
|
||||||
message = new Array(conf.len + 1).join('a');
|
message = 'a'.repeat(conf.len);
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
message = new Array(conf.len / 2 + 1).join('ü');
|
message = 'ü'.repeat(conf.len / 2);
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
break;
|
break;
|
||||||
case 'buf':
|
case 'buf':
|
||||||
|
@ -24,11 +24,11 @@ function main(conf) {
|
|||||||
var encoding;
|
var encoding;
|
||||||
switch (conf.type) {
|
switch (conf.type) {
|
||||||
case 'asc':
|
case 'asc':
|
||||||
message = new Array(conf.len + 1).join('a');
|
message = 'a'.repeat(conf.len);
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
message = new Array(conf.len / 2 + 1).join('ü');
|
message = 'ü'.repeat(conf.len / 2);
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
break;
|
break;
|
||||||
case 'buf':
|
case 'buf':
|
||||||
|
35
benchmark/es/string-repeat.js
Normal file
35
benchmark/es/string-repeat.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
'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;
|
||||||
|
|
||||||
|
if (conf.mode === 'Array') {
|
||||||
|
bench.start();
|
||||||
|
for (let i = 0; i < n; i++)
|
||||||
|
str = new Array(size + 1).join(character);
|
||||||
|
bench.end(n);
|
||||||
|
} else {
|
||||||
|
bench.start();
|
||||||
|
for (let i = 0; i < n; i++)
|
||||||
|
str = character.repeat(size);
|
||||||
|
bench.end(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.strictEqual([...str].length, size);
|
||||||
|
}
|
@ -24,11 +24,11 @@ function main(conf) {
|
|||||||
chunk = Buffer.alloc(size, 'b');
|
chunk = Buffer.alloc(size, 'b');
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(size + 1).join('a');
|
chunk = 'a'.repeat(size);
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
chunk = new Array(Math.ceil(size / 2) + 1).join('ü');
|
chunk = 'ü'.repeat(Math.ceil(size / 2));
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -23,10 +23,10 @@ function main(conf) {
|
|||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(len + 1).join('a');
|
chunk = 'a'.repeat(len);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,10 +26,10 @@ function main(conf) {
|
|||||||
chunk = Buffer.alloc(len, 'x');
|
chunk = Buffer.alloc(len, 'x');
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(len + 1).join('a');
|
chunk = 'a'.repeat(len);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,11 +27,11 @@ function main(conf) {
|
|||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -27,11 +27,11 @@ function main(conf) {
|
|||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -27,11 +27,11 @@ function main(conf) {
|
|||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -27,11 +27,11 @@ function main(conf) {
|
|||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -83,10 +83,10 @@ function client() {
|
|||||||
chunk = Buffer.alloc(len, 'x');
|
chunk = Buffer.alloc(len, 'x');
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -80,10 +80,10 @@ function client() {
|
|||||||
chunk = Buffer.alloc(len, 'x');
|
chunk = Buffer.alloc(len, 'x');
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -54,10 +54,10 @@ function server() {
|
|||||||
chunk = Buffer.alloc(len, 'x');
|
chunk = Buffer.alloc(len, 'x');
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
chunk = new Array(len / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(len / 2);
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(len + 1).join('x');
|
chunk = 'x'.repeat(len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid type: ' + type);
|
throw new Error('invalid type: ' + type);
|
||||||
|
@ -26,11 +26,11 @@ function main(conf) {
|
|||||||
chunk = Buffer.alloc(size, 'b');
|
chunk = Buffer.alloc(size, 'b');
|
||||||
break;
|
break;
|
||||||
case 'asc':
|
case 'asc':
|
||||||
chunk = new Array(size + 1).join('a');
|
chunk = 'a'.repeat(size);
|
||||||
encoding = 'ascii';
|
encoding = 'ascii';
|
||||||
break;
|
break;
|
||||||
case 'utf':
|
case 'utf':
|
||||||
chunk = new Array(size / 2 + 1).join('ü');
|
chunk = 'ü'.repeat(size / 2);
|
||||||
encoding = 'utf8';
|
encoding = 'utf8';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user