-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add a benchmark for read() of ReadableStreams
Refs: nodejs/performance#82 PR-URL: #49622 Reviewed-By: Yagiz Nizipli <[email protected]>
- Loading branch information
1 parent
462228b
commit 4879e3f
Showing
1 changed file
with
49 additions
and
0 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,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; | ||
} | ||
} | ||
} |