-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: simpler and faster Readable async iterator
Reimplement as an async generator instead of a custom iterator class. PR-URL: #34035 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
7 changed files
with
125 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const Readable = require('stream').Readable; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e5], | ||
sync: ['yes', 'no'], | ||
}); | ||
|
||
async function main({ n, sync }) { | ||
sync = sync === 'yes'; | ||
|
||
const s = new Readable({ | ||
objectMode: true, | ||
read() { | ||
if (sync) { | ||
this.push(1); | ||
} else { | ||
process.nextTick(() => { | ||
this.push(1); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
bench.start(); | ||
|
||
let x = 0; | ||
for await (const chunk of s) { | ||
x += chunk; | ||
if (x > n) { | ||
break; | ||
} | ||
} | ||
|
||
// Side effect to ensure V8 does not optimize away the | ||
// loop as a noop. | ||
console.log(x); | ||
|
||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.