File tree 15 files changed +63
-28
lines changed
15 files changed +63
-28
lines changed Original file line number Diff line number Diff line change @@ -40,11 +40,11 @@ function main(conf) {
40
40
var encoding ;
41
41
switch ( conf . type ) {
42
42
case 'asc' :
43
- message = new Array ( conf . len + 1 ) . join ( 'a' ) ;
43
+ message = 'a' . repeat ( conf . len ) ;
44
44
encoding = 'ascii' ;
45
45
break ;
46
46
case 'utf' :
47
- message = new Array ( conf . len / 2 + 1 ) . join ( 'ü' ) ;
47
+ message = 'ü' . repeat ( conf . len / 2 ) ;
48
48
encoding = 'utf8' ;
49
49
break ;
50
50
case 'buf' :
Original file line number Diff line number Diff line change @@ -25,11 +25,11 @@ function main(conf) {
25
25
var encoding ;
26
26
switch ( conf . type ) {
27
27
case 'asc' :
28
- message = new Array ( conf . len + 1 ) . join ( 'a' ) ;
28
+ message = 'a' . repeat ( conf . len ) ;
29
29
encoding = 'ascii' ;
30
30
break ;
31
31
case 'utf' :
32
- message = new Array ( conf . len / 2 + 1 ) . join ( 'ü' ) ;
32
+ message = 'ü' . repeat ( conf . len / 2 ) ;
33
33
encoding = 'utf8' ;
34
34
break ;
35
35
case 'buf' :
Original file line number Diff line number Diff line change @@ -24,11 +24,11 @@ function main(conf) {
24
24
var encoding ;
25
25
switch ( conf . type ) {
26
26
case 'asc' :
27
- message = new Array ( conf . len + 1 ) . join ( 'a' ) ;
27
+ message = 'a' . repeat ( conf . len ) ;
28
28
encoding = 'ascii' ;
29
29
break ;
30
30
case 'utf' :
31
- message = new Array ( conf . len / 2 + 1 ) . join ( 'ü' ) ;
31
+ message = 'ü' . repeat ( conf . len / 2 ) ;
32
32
encoding = 'utf8' ;
33
33
break ;
34
34
case 'buf' :
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -24,11 +24,11 @@ function main(conf) {
24
24
chunk = Buffer . alloc ( size , 'b' ) ;
25
25
break ;
26
26
case 'asc' :
27
- chunk = new Array ( size + 1 ) . join ( 'a' ) ;
27
+ chunk = 'a' . repeat ( size ) ;
28
28
encoding = 'ascii' ;
29
29
break ;
30
30
case 'utf' :
31
- chunk = new Array ( Math . ceil ( size / 2 ) + 1 ) . join ( 'ü' ) ;
31
+ chunk = 'ü' . repeat ( Math . ceil ( size / 2 ) ) ;
32
32
encoding = 'utf8' ;
33
33
break ;
34
34
default :
Original file line number Diff line number Diff line change @@ -23,10 +23,10 @@ function main(conf) {
23
23
break ;
24
24
case 'utf' :
25
25
encoding = 'utf8' ;
26
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
26
+ chunk = 'ü' . repeat ( len / 2 ) ;
27
27
break ;
28
28
case 'asc' :
29
- chunk = new Array ( len + 1 ) . join ( 'a' ) ;
29
+ chunk = 'a' . repeat ( len ) ;
30
30
break ;
31
31
}
32
32
Original file line number Diff line number Diff line change @@ -26,10 +26,10 @@ function main(conf) {
26
26
chunk = Buffer . alloc ( len , 'x' ) ;
27
27
break ;
28
28
case 'utf' :
29
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
29
+ chunk = 'ü' . repeat ( len / 2 ) ;
30
30
break ;
31
31
case 'asc' :
32
- chunk = new Array ( len + 1 ) . join ( 'a' ) ;
32
+ chunk = 'a' . repeat ( len ) ;
33
33
break ;
34
34
}
35
35
Original file line number Diff line number Diff line change @@ -27,11 +27,11 @@ function main(conf) {
27
27
break ;
28
28
case 'utf' :
29
29
encoding = 'utf8' ;
30
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
30
+ chunk = 'ü' . repeat ( len / 2 ) ;
31
31
break ;
32
32
case 'asc' :
33
33
encoding = 'ascii' ;
34
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
34
+ chunk = 'x' . repeat ( len ) ;
35
35
break ;
36
36
default :
37
37
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -27,11 +27,11 @@ function main(conf) {
27
27
break ;
28
28
case 'utf' :
29
29
encoding = 'utf8' ;
30
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
30
+ chunk = 'ü' . repeat ( len / 2 ) ;
31
31
break ;
32
32
case 'asc' :
33
33
encoding = 'ascii' ;
34
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
34
+ chunk = 'x' . repeat ( len ) ;
35
35
break ;
36
36
default :
37
37
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -27,11 +27,11 @@ function main(conf) {
27
27
break ;
28
28
case 'utf' :
29
29
encoding = 'utf8' ;
30
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
30
+ chunk = 'ü' . repeat ( len / 2 ) ;
31
31
break ;
32
32
case 'asc' :
33
33
encoding = 'ascii' ;
34
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
34
+ chunk = 'x' . repeat ( len ) ;
35
35
break ;
36
36
default :
37
37
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -27,11 +27,11 @@ function main(conf) {
27
27
break ;
28
28
case 'utf' :
29
29
encoding = 'utf8' ;
30
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
30
+ chunk = 'ü' . repeat ( len / 2 ) ;
31
31
break ;
32
32
case 'asc' :
33
33
encoding = 'ascii' ;
34
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
34
+ chunk = 'x' . repeat ( len ) ;
35
35
break ;
36
36
default :
37
37
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -83,10 +83,10 @@ function client() {
83
83
chunk = Buffer . alloc ( len , 'x' ) ;
84
84
break ;
85
85
case 'utf' :
86
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
86
+ chunk = 'ü' . repeat ( len / 2 ) ;
87
87
break ;
88
88
case 'asc' :
89
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
89
+ chunk = 'x' . repeat ( len ) ;
90
90
break ;
91
91
default :
92
92
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -80,10 +80,10 @@ function client() {
80
80
chunk = Buffer . alloc ( len , 'x' ) ;
81
81
break ;
82
82
case 'utf' :
83
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
83
+ chunk = 'ü' . repeat ( len / 2 ) ;
84
84
break ;
85
85
case 'asc' :
86
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
86
+ chunk = 'x' . repeat ( len ) ;
87
87
break ;
88
88
default :
89
89
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -54,10 +54,10 @@ function server() {
54
54
chunk = Buffer . alloc ( len , 'x' ) ;
55
55
break ;
56
56
case 'utf' :
57
- chunk = new Array ( len / 2 + 1 ) . join ( 'ü' ) ;
57
+ chunk = 'ü' . repeat ( len / 2 ) ;
58
58
break ;
59
59
case 'asc' :
60
- chunk = new Array ( len + 1 ) . join ( 'x' ) ;
60
+ chunk = 'x' . repeat ( len ) ;
61
61
break ;
62
62
default :
63
63
throw new Error ( 'invalid type: ' + type ) ;
Original file line number Diff line number Diff line change @@ -26,11 +26,11 @@ function main(conf) {
26
26
chunk = Buffer . alloc ( size , 'b' ) ;
27
27
break ;
28
28
case 'asc' :
29
- chunk = new Array ( size + 1 ) . join ( 'a' ) ;
29
+ chunk = 'a' . repeat ( size ) ;
30
30
encoding = 'ascii' ;
31
31
break ;
32
32
case 'utf' :
33
- chunk = new Array ( size / 2 + 1 ) . join ( 'ü' ) ;
33
+ chunk = 'ü' . repeat ( size / 2 ) ;
34
34
encoding = 'utf8' ;
35
35
break ;
36
36
default :
You can’t perform that action at this time.
0 commit comments