File tree 3 files changed +109
-0
lines changed
3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const common = require ( '../common.js' ) ;
3
+ const {
4
+ ReadableStream,
5
+ TransformStream,
6
+ WritableStream,
7
+ } = require ( 'node:stream/web' ) ;
8
+
9
+ const bench = common . createBenchmark ( main , {
10
+ n : [ 50e3 ] ,
11
+ kind : [ 'ReadableStream' , 'TransformStream' , 'WritableStream' ]
12
+ } ) ;
13
+
14
+ function main ( { n, kind } ) {
15
+ switch ( kind ) {
16
+ case 'ReadableStream' :
17
+ new ReadableStream ( { } ) ;
18
+ new ReadableStream ( ) ;
19
+
20
+ bench . start ( ) ;
21
+ for ( let i = 0 ; i < n ; ++ i )
22
+ new ReadableStream ( ) . cancel ( ) ;
23
+ bench . end ( n ) ;
24
+ break ;
25
+ case 'WritableStream' :
26
+ new WritableStream ( { } ) ;
27
+ new WritableStream ( ) ;
28
+
29
+ bench . start ( ) ;
30
+ for ( let i = 0 ; i < n ; ++ i )
31
+ new WritableStream ( ) ;
32
+ bench . end ( n ) ;
33
+ break ;
34
+ case 'TransformStream' :
35
+ new TransformStream ( { } ) ;
36
+ new TransformStream ( ) ;
37
+
38
+ bench . start ( ) ;
39
+ for ( let i = 0 ; i < n ; ++ i )
40
+ new TransformStream ( ) ;
41
+ bench . end ( n ) ;
42
+ break ;
43
+ default :
44
+ throw new Error ( 'Invalid kind' ) ;
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const common = require ( '../common.js' ) ;
3
+ const {
4
+ ReadableStream,
5
+ WritableStream,
6
+ } = require ( 'node:stream/web' ) ;
7
+
8
+ const bench = common . createBenchmark ( main , {
9
+ n : [ 5e6 ] ,
10
+ } ) ;
11
+
12
+
13
+ async function main ( { n } ) {
14
+ const b = Buffer . alloc ( 1024 ) ;
15
+ let i = 0 ;
16
+ const rs = new ReadableStream ( {
17
+ pull : function ( controller ) {
18
+ if ( i ++ === n ) {
19
+ controller . enqueue ( b ) ;
20
+ } else {
21
+ controller . close ( ) ;
22
+ }
23
+ }
24
+ } ) ;
25
+ const ws = new WritableStream ( {
26
+ write ( chunk , controller ) { } ,
27
+ close ( ) { bench . end ( n ) ; } ,
28
+ } ) ;
29
+
30
+ bench . start ( ) ;
31
+ rs . pipeTo ( ws ) ;
32
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const common = require ( '../common.js' ) ;
3
+ const {
4
+ ReadableStream,
5
+ } = require ( 'node:stream/web' ) ;
6
+
7
+ const bench = common . createBenchmark ( main , {
8
+ n : [ 1e5 ] ,
9
+ } ) ;
10
+
11
+
12
+ async function main ( { n } ) {
13
+ const rs = new ReadableStream ( {
14
+ pull : function ( controller ) {
15
+ controller . enqueue ( 1 ) ;
16
+ }
17
+ } ) ;
18
+
19
+ let x = 0 ;
20
+ for await ( const chunk of rs ) {
21
+ x += chunk ;
22
+ if ( x > n ) {
23
+ break ;
24
+ }
25
+ }
26
+
27
+ bench . start ( ) ;
28
+ // Use x to ensure V8 does not optimize away the loop as a noop.
29
+ console . assert ( x ) ;
30
+ bench . end ( n ) ;
31
+ }
You can’t perform that action at this time.
0 commit comments