From f057cc3d84d8070de455e58bec650273ed952295 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Mon, 3 Apr 2017 00:32:50 +0300 Subject: [PATCH] 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 Reviewed-By: James M Snell --- benchmark/crypto/cipher-stream.js | 4 +-- benchmark/crypto/hash-stream-creation.js | 4 +-- benchmark/crypto/hash-stream-throughput.js | 4 +-- benchmark/es/string-repeat.js | 35 ++++++++++++++++++++++ benchmark/fs/write-stream-throughput.js | 4 +-- benchmark/http/client-request-body.js | 4 +-- benchmark/http/end-vs-write-end.js | 4 +-- benchmark/net/net-c2s-cork.js | 4 +-- benchmark/net/net-c2s.js | 4 +-- benchmark/net/net-pipe.js | 4 +-- benchmark/net/net-s2c.js | 4 +-- benchmark/net/tcp-raw-c2s.js | 4 +-- benchmark/net/tcp-raw-pipe.js | 4 +-- benchmark/net/tcp-raw-s2c.js | 4 +-- benchmark/tls/throughput.js | 4 +-- 15 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 benchmark/es/string-repeat.js diff --git a/benchmark/crypto/cipher-stream.js b/benchmark/crypto/cipher-stream.js index 11e2c38c0b..03780ba130 100644 --- a/benchmark/crypto/cipher-stream.js +++ b/benchmark/crypto/cipher-stream.js @@ -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': diff --git a/benchmark/crypto/hash-stream-creation.js b/benchmark/crypto/hash-stream-creation.js index e7fcde3aa6..3be09785ac 100644 --- a/benchmark/crypto/hash-stream-creation.js +++ b/benchmark/crypto/hash-stream-creation.js @@ -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': diff --git a/benchmark/crypto/hash-stream-throughput.js b/benchmark/crypto/hash-stream-throughput.js index 732c2b89c7..bf426fc2c9 100644 --- a/benchmark/crypto/hash-stream-throughput.js +++ b/benchmark/crypto/hash-stream-throughput.js @@ -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': diff --git a/benchmark/es/string-repeat.js b/benchmark/es/string-repeat.js new file mode 100644 index 0000000000..a6b389033a --- /dev/null +++ b/benchmark/es/string-repeat.js @@ -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); +} diff --git a/benchmark/fs/write-stream-throughput.js b/benchmark/fs/write-stream-throughput.js index 812dc369d7..3e54c09199 100644 --- a/benchmark/fs/write-stream-throughput.js +++ b/benchmark/fs/write-stream-throughput.js @@ -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: diff --git a/benchmark/http/client-request-body.js b/benchmark/http/client-request-body.js index f6b5ab1919..ab7e3877f3 100644 --- a/benchmark/http/client-request-body.js +++ b/benchmark/http/client-request-body.js @@ -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; } diff --git a/benchmark/http/end-vs-write-end.js b/benchmark/http/end-vs-write-end.js index 62b1a6a097..3c216e766c 100644 --- a/benchmark/http/end-vs-write-end.js +++ b/benchmark/http/end-vs-write-end.js @@ -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; } diff --git a/benchmark/net/net-c2s-cork.js b/benchmark/net/net-c2s-cork.js index 6af9162025..4a119e9c27 100644 --- a/benchmark/net/net-c2s-cork.js +++ b/benchmark/net/net-c2s-cork.js @@ -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); diff --git a/benchmark/net/net-c2s.js b/benchmark/net/net-c2s.js index 7e59bc528b..fdc5cfc5c7 100644 --- a/benchmark/net/net-c2s.js +++ b/benchmark/net/net-c2s.js @@ -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); diff --git a/benchmark/net/net-pipe.js b/benchmark/net/net-pipe.js index 7d4849c4ef..d40da7e549 100644 --- a/benchmark/net/net-pipe.js +++ b/benchmark/net/net-pipe.js @@ -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); diff --git a/benchmark/net/net-s2c.js b/benchmark/net/net-s2c.js index a4a5b4ab49..1c104e3417 100644 --- a/benchmark/net/net-s2c.js +++ b/benchmark/net/net-s2c.js @@ -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); diff --git a/benchmark/net/tcp-raw-c2s.js b/benchmark/net/tcp-raw-c2s.js index c33c6d0f2a..8c9eff76e9 100644 --- a/benchmark/net/tcp-raw-c2s.js +++ b/benchmark/net/tcp-raw-c2s.js @@ -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); diff --git a/benchmark/net/tcp-raw-pipe.js b/benchmark/net/tcp-raw-pipe.js index b7c6776c95..0501d13f00 100644 --- a/benchmark/net/tcp-raw-pipe.js +++ b/benchmark/net/tcp-raw-pipe.js @@ -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); diff --git a/benchmark/net/tcp-raw-s2c.js b/benchmark/net/tcp-raw-s2c.js index a7eeed1921..1cb0fb63f4 100644 --- a/benchmark/net/tcp-raw-s2c.js +++ b/benchmark/net/tcp-raw-s2c.js @@ -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); diff --git a/benchmark/tls/throughput.js b/benchmark/tls/throughput.js index d3b7d0c022..c2b389fe45 100644 --- a/benchmark/tls/throughput.js +++ b/benchmark/tls/throughput.js @@ -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: