Skip to content

Commit 158f6f7

Browse files
mcollinaaduh95
authored andcommitted
stream: share webstreams async iterator methods
ReadableStream.prototype.values() built each iterator from an object literal with a computed symbol-key method plus five closures. Such a literal is rebuilt through the runtime on every evaluation, costing close to a microsecond per iterator, which dominates iterating a short-lived stream. Move next() and return() to a shared ReadableStreamAsyncIterator prototype, as for any WebIDL async iterator, and keep the per-iterator state in its read request. The prototype chain and property shape are the ones WPT checks; the placeholder AsyncIterator object in util.js is no longer needed. As in WebIDL, next() and return() now reject when called on something that is not a ReadableStream async iterator, and iterators no longer carry own next/return properties. Add an async-iterator kind to benchmark/webstreams/lifecycle.js. webstreams/lifecycle.js kind='async-iterator' *** +16.22% Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #66392 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent aff6017 commit 158f6f7

4 files changed

Lines changed: 239 additions & 158 deletions

File tree

‎benchmark/webstreams/lifecycle.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99

1010
const bench = common.createBenchmark(main, {
1111
n: [5e4],
12-
kind: ['readable', 'pipe-to', 'pipe-through'],
12+
kind: ['readable', 'async-iterator', 'pipe-to', 'pipe-through'],
1313
});
1414

1515
const chunk = Buffer.alloc(1024);
@@ -37,6 +37,18 @@ async function readable(n) {
3737
assert.strictEqual(chunks, n * 4);
3838
}
3939

40+
async function asyncIterator(n) {
41+
let chunks = 0;
42+
bench.start();
43+
for (let i = 0; i < n; i++) {
44+
for await (const chunk of new ReadableStream(makeSource())) {
45+
if (chunk) chunks++;
46+
}
47+
}
48+
bench.end(n);
49+
assert.strictEqual(chunks, n * 4);
50+
}
51+
4052
async function pipeTo(n) {
4153
let chunks = 0;
4254
bench.start();
@@ -66,6 +78,9 @@ function main({ n, kind }) {
6678
case 'readable':
6779
readable(n);
6880
break;
81+
case 'async-iterator':
82+
asyncIterator(n);
83+
break;
6984
case 'pipe-to':
7085
pipeTo(n);
7186
break;

0 commit comments

Comments
 (0)