Skip to content

Commit

Permalink
benchmark: replace [].join() with ''.repeat()
Browse files Browse the repository at this point in the history
Also add a benchmark to compare both ways to create strings.

PR-URL: #12170
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
vsemozhetbyt authored and jasnell committed Apr 4, 2017
1 parent 9348f31 commit 74dc3bf
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 28 deletions.
4 changes: 2 additions & 2 deletions benchmark/crypto/cipher-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ function main(conf) {
var encoding;
switch (conf.type) {
case 'asc':
message = new Array(conf.len + 1).join('a');
message = 'a'.repeat(conf.len);
encoding = 'ascii';
break;
case 'utf':
message = new Array(conf.len / 2 + 1).join('ü');
message = 'ü'.repeat(conf.len / 2);
encoding = 'utf8';
break;
case 'buf':
Expand Down
4 changes: 2 additions & 2 deletions benchmark/crypto/hash-stream-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ function main(conf) {
var encoding;
switch (conf.type) {
case 'asc':
message = new Array(conf.len + 1).join('a');
message = 'a'.repeat(conf.len);
encoding = 'ascii';
break;
case 'utf':
message = new Array(conf.len / 2 + 1).join('ü');
message = 'ü'.repeat(conf.len / 2);
encoding = 'utf8';
break;
case 'buf':
Expand Down
4 changes: 2 additions & 2 deletions benchmark/crypto/hash-stream-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function main(conf) {
var encoding;
switch (conf.type) {
case 'asc':
message = new Array(conf.len + 1).join('a');
message = 'a'.repeat(conf.len);
encoding = 'ascii';
break;
case 'utf':
message = new Array(conf.len / 2 + 1).join('ü');
message = 'ü'.repeat(conf.len / 2);
encoding = 'utf8';
break;
case 'buf':
Expand Down
35 changes: 35 additions & 0 deletions benchmark/es/string-repeat.js
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions benchmark/fs/write-stream-throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function main(conf) {
chunk = Buffer.alloc(size, 'b');
break;
case 'asc':
chunk = new Array(size + 1).join('a');
chunk = 'a'.repeat(size);
encoding = 'ascii';
break;
case 'utf':
chunk = new Array(Math.ceil(size / 2) + 1).join('ü');
chunk = 'ü'.repeat(Math.ceil(size / 2));
encoding = 'utf8';
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions benchmark/http/client-request-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ function main(conf) {
break;
case 'utf':
encoding = 'utf8';
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
chunk = new Array(len + 1).join('a');
chunk = 'a'.repeat(len);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/http/end-vs-write-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function main(conf) {
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
chunk = new Array(len + 1).join('a');
chunk = 'a'.repeat(len);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/net-c2s-cork.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function main(conf) {
break;
case 'utf':
encoding = 'utf8';
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
encoding = 'ascii';
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/net-c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function main(conf) {
break;
case 'utf':
encoding = 'utf8';
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
encoding = 'ascii';
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/net-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function main(conf) {
break;
case 'utf':
encoding = 'utf8';
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
encoding = 'ascii';
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/net-s2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function main(conf) {
break;
case 'utf':
encoding = 'utf8';
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
encoding = 'ascii';
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/tcp-raw-c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ function client() {
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/tcp-raw-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ function client() {
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/net/tcp-raw-s2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ function server() {
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
chunk = new Array(len / 2 + 1).join('ü');
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
chunk = new Array(len + 1).join('x');
chunk = 'x'.repeat(len);
break;
default:
throw new Error('invalid type: ' + type);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/tls/throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function main(conf) {
chunk = Buffer.alloc(size, 'b');
break;
case 'asc':
chunk = new Array(size + 1).join('a');
chunk = 'a'.repeat(size);
encoding = 'ascii';
break;
case 'utf':
chunk = new Array(size / 2 + 1).join('ü');
chunk = 'ü'.repeat(size / 2);
encoding = 'utf8';
break;
default:
Expand Down

0 comments on commit 74dc3bf

Please sign in to comment.