File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const common = require ( '../common.js' ) ;
3
+ const { ReadableStream } = require ( 'node:stream/web' ) ;
4
+
5
+ const bench = common . createBenchmark ( main , {
6
+ n : [ 1e5 ] ,
7
+ type : [ 'normal' , 'byob' ] ,
8
+ } ) ;
9
+
10
+ async function main ( { n, type } ) {
11
+ switch ( type ) {
12
+ case 'normal' : {
13
+ const rs = new ReadableStream ( {
14
+ pull : function ( controller ) {
15
+ controller . enqueue ( 'a' ) ;
16
+ } ,
17
+ } ) ;
18
+ const reader = rs . getReader ( ) ;
19
+ let x = null ;
20
+ bench . start ( ) ;
21
+ for ( let i = 0 ; i < n ; i ++ ) {
22
+ const { value } = await reader . read ( ) ;
23
+ x = value ;
24
+ }
25
+ bench . end ( n ) ;
26
+ console . assert ( x ) ;
27
+ break ;
28
+ }
29
+ case 'byob' : {
30
+ const encode = new TextEncoder ( ) ;
31
+ const rs = new ReadableStream ( {
32
+ type : 'bytes' ,
33
+ pull : function ( controller ) {
34
+ controller . enqueue ( encode . encode ( 'a' ) ) ;
35
+ } ,
36
+ } ) ;
37
+ const reader = rs . getReader ( { mode : 'byob' } ) ;
38
+ let x = null ;
39
+ bench . start ( ) ;
40
+ for ( let i = 0 ; i < n ; i ++ ) {
41
+ const { value } = await reader . read ( new Uint8Array ( 1 ) ) ;
42
+ x = value ;
43
+ }
44
+ bench . end ( n ) ;
45
+ console . assert ( x ) ;
46
+ break ;
47
+ }
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments