File tree Expand file tree Collapse file tree 4 files changed +119
-0
lines changed Expand file tree Collapse file tree 4 files changed +119
-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+ const assert = require ( 'assert' ) ;
9+
10+ const bench = common . createBenchmark ( main , {
11+ n : [ 50e3 ] ,
12+ kind : [ 'ReadableStream' , 'TransformStream' , 'WritableStream' ]
13+ } ) ;
14+
15+ let rs , ws , ts ;
16+
17+ function main ( { n, kind } ) {
18+ switch ( kind ) {
19+ case 'ReadableStream' :
20+ bench . start ( ) ;
21+ for ( let i = 0 ; i < n ; ++ i )
22+ rs = new ReadableStream ( ) ;
23+ bench . end ( n ) ;
24+
25+ // Avoid V8 deadcode (elimination)
26+ assert . ok ( rs ) ;
27+ break ;
28+ case 'WritableStream' :
29+ bench . start ( ) ;
30+ for ( let i = 0 ; i < n ; ++ i )
31+ ws = new WritableStream ( ) ;
32+ bench . end ( n ) ;
33+
34+ // Avoid V8 deadcode (elimination)
35+ assert . ok ( ws ) ;
36+ break ;
37+ case 'TransformStream' :
38+ bench . start ( ) ;
39+ for ( let i = 0 ; i < n ; ++ i )
40+ ts = new TransformStream ( ) ;
41+ bench . end ( n ) ;
42+
43+ // Avoid V8 deadcode (elimination)
44+ assert . ok ( ts ) ;
45+ break ;
46+ default :
47+ throw new Error ( 'Invalid kind' ) ;
48+ }
49+ }
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+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ require ( '../common' ) ;
4+
5+ const runBenchmark = require ( '../common/benchmark' ) ;
6+
7+ runBenchmark ( 'webstreams' , { NODEJS_BENCHMARK_ZERO_ALLOWED : 1 } ) ;
You can’t perform that action at this time.
0 commit comments