Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f057cc3

Browse filesBrowse files
vsemozhetbytItalo A. Casas
authored andcommitted
benchmark: replace [].join() with ''.repeat()
Also add a benchmark to compare both ways to create strings. PR-URL: #12170 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b15dc95 commit f057cc3
Copy full SHA for f057cc3
Expand file treeCollapse file tree

15 files changed

+63
-28
lines changed
Open diff view settings
Collapse file

‎benchmark/crypto/cipher-stream.js‎

Copy file name to clipboardExpand all lines: benchmark/crypto/cipher-stream.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ function main(conf) {
4040
var encoding;
4141
switch (conf.type) {
4242
case 'asc':
43-
message = new Array(conf.len + 1).join('a');
43+
message = 'a'.repeat(conf.len);
4444
encoding = 'ascii';
4545
break;
4646
case 'utf':
47-
message = new Array(conf.len / 2 + 1).join('ü');
47+
message = 'ü'.repeat(conf.len / 2);
4848
encoding = 'utf8';
4949
break;
5050
case 'buf':
Collapse file

‎benchmark/crypto/hash-stream-creation.js‎

Copy file name to clipboardExpand all lines: benchmark/crypto/hash-stream-creation.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ function main(conf) {
2525
var encoding;
2626
switch (conf.type) {
2727
case 'asc':
28-
message = new Array(conf.len + 1).join('a');
28+
message = 'a'.repeat(conf.len);
2929
encoding = 'ascii';
3030
break;
3131
case 'utf':
32-
message = new Array(conf.len / 2 + 1).join('ü');
32+
message = 'ü'.repeat(conf.len / 2);
3333
encoding = 'utf8';
3434
break;
3535
case 'buf':
Collapse file

‎benchmark/crypto/hash-stream-throughput.js‎

Copy file name to clipboardExpand all lines: benchmark/crypto/hash-stream-throughput.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ function main(conf) {
2424
var encoding;
2525
switch (conf.type) {
2626
case 'asc':
27-
message = new Array(conf.len + 1).join('a');
27+
message = 'a'.repeat(conf.len);
2828
encoding = 'ascii';
2929
break;
3030
case 'utf':
31-
message = new Array(conf.len / 2 + 1).join('ü');
31+
message = 'ü'.repeat(conf.len / 2);
3232
encoding = 'utf8';
3333
break;
3434
case 'buf':
Collapse file

‎benchmark/es/string-repeat.js‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const configs = {
7+
n: [1e3],
8+
mode: ['Array', 'repeat'],
9+
encoding: ['ascii', 'utf8'],
10+
size: [1e1, 1e3, 1e6],
11+
};
12+
13+
const bench = common.createBenchmark(main, configs);
14+
15+
function main(conf) {
16+
const n = +conf.n;
17+
const size = +conf.size;
18+
const character = conf.encoding === 'ascii' ? 'a' : '\ud83d\udc0e'; // '🐎'
19+
20+
let str;
21+
22+
if (conf.mode === 'Array') {
23+
bench.start();
24+
for (let i = 0; i < n; i++)
25+
str = new Array(size + 1).join(character);
26+
bench.end(n);
27+
} else {
28+
bench.start();
29+
for (let i = 0; i < n; i++)
30+
str = character.repeat(size);
31+
bench.end(n);
32+
}
33+
34+
assert.strictEqual([...str].length, size);
35+
}
Collapse file

‎benchmark/fs/write-stream-throughput.js‎

Copy file name to clipboardExpand all lines: benchmark/fs/write-stream-throughput.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ function main(conf) {
2424
chunk = Buffer.alloc(size, 'b');
2525
break;
2626
case 'asc':
27-
chunk = new Array(size + 1).join('a');
27+
chunk = 'a'.repeat(size);
2828
encoding = 'ascii';
2929
break;
3030
case 'utf':
31-
chunk = new Array(Math.ceil(size / 2) + 1).join('ü');
31+
chunk = 'ü'.repeat(Math.ceil(size / 2));
3232
encoding = 'utf8';
3333
break;
3434
default:
Collapse file

‎benchmark/http/client-request-body.js‎

Copy file name to clipboardExpand all lines: benchmark/http/client-request-body.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ function main(conf) {
2323
break;
2424
case 'utf':
2525
encoding = 'utf8';
26-
chunk = new Array(len / 2 + 1).join('ü');
26+
chunk = 'ü'.repeat(len / 2);
2727
break;
2828
case 'asc':
29-
chunk = new Array(len + 1).join('a');
29+
chunk = 'a'.repeat(len);
3030
break;
3131
}
3232

Collapse file

‎benchmark/http/end-vs-write-end.js‎

Copy file name to clipboardExpand all lines: benchmark/http/end-vs-write-end.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ function main(conf) {
2626
chunk = Buffer.alloc(len, 'x');
2727
break;
2828
case 'utf':
29-
chunk = new Array(len / 2 + 1).join('ü');
29+
chunk = 'ü'.repeat(len / 2);
3030
break;
3131
case 'asc':
32-
chunk = new Array(len + 1).join('a');
32+
chunk = 'a'.repeat(len);
3333
break;
3434
}
3535

Collapse file

‎benchmark/net/net-c2s-cork.js‎

Copy file name to clipboardExpand all lines: benchmark/net/net-c2s-cork.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function main(conf) {
2727
break;
2828
case 'utf':
2929
encoding = 'utf8';
30-
chunk = new Array(len / 2 + 1).join('ü');
30+
chunk = 'ü'.repeat(len / 2);
3131
break;
3232
case 'asc':
3333
encoding = 'ascii';
34-
chunk = new Array(len + 1).join('x');
34+
chunk = 'x'.repeat(len);
3535
break;
3636
default:
3737
throw new Error('invalid type: ' + type);
Collapse file

‎benchmark/net/net-c2s.js‎

Copy file name to clipboardExpand all lines: benchmark/net/net-c2s.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function main(conf) {
2727
break;
2828
case 'utf':
2929
encoding = 'utf8';
30-
chunk = new Array(len / 2 + 1).join('ü');
30+
chunk = 'ü'.repeat(len / 2);
3131
break;
3232
case 'asc':
3333
encoding = 'ascii';
34-
chunk = new Array(len + 1).join('x');
34+
chunk = 'x'.repeat(len);
3535
break;
3636
default:
3737
throw new Error('invalid type: ' + type);
Collapse file

‎benchmark/net/net-pipe.js‎

Copy file name to clipboardExpand all lines: benchmark/net/net-pipe.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function main(conf) {
2727
break;
2828
case 'utf':
2929
encoding = 'utf8';
30-
chunk = new Array(len / 2 + 1).join('ü');
30+
chunk = 'ü'.repeat(len / 2);
3131
break;
3232
case 'asc':
3333
encoding = 'ascii';
34-
chunk = new Array(len + 1).join('x');
34+
chunk = 'x'.repeat(len);
3535
break;
3636
default:
3737
throw new Error('invalid type: ' + type);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.