forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadable-read.js
More file actions
49 lines (47 loc) · 1.15 KB
/
readable-read.js
File metadata and controls
49 lines (47 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const common = require('../common.js');
const { ReadableStream } = require('node:stream/web');
const bench = common.createBenchmark(main, {
n: [1e5],
type: ['normal', 'byob'],
});
async function main({ n, type }) {
switch (type) {
case 'normal': {
const rs = new ReadableStream({
pull: function(controller) {
controller.enqueue('a');
},
});
const reader = rs.getReader();
let x = null;
bench.start();
for (let i = 0; i < n; i++) {
const { value } = await reader.read();
x = value;
}
bench.end(n);
console.assert(x);
break;
}
case 'byob': {
const encode = new TextEncoder();
const rs = new ReadableStream({
type: 'bytes',
pull: function(controller) {
controller.enqueue(encode.encode('a'));
},
});
const reader = rs.getReader({ mode: 'byob' });
let x = null;
bench.start();
for (let i = 0; i < n; i++) {
const { value } = await reader.read(new Uint8Array(1));
x = value;
}
bench.end(n);
console.assert(x);
break;
}
}
}