| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fafac6e commit e024712
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,44 @@ | |||
| 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', 'bytes'], | ||
| 8 | + }); | ||
| 9 | + | ||
| 10 | + async function main({ n, type }) { | ||
| 11 | + let i = 0; | ||
| 12 | + const source = type === 'bytes' ? | ||
| 13 | + { | ||
| 14 | + type: 'bytes', | ||
| 15 | + pull(controller) { | ||
| 16 | + if (i++ < n) controller.enqueue(new Uint8Array(16)); | ||
| 17 | + else controller.close(); | ||
| 18 | + }, | ||
| 19 | + } : | ||
| 20 | + { | ||
| 21 | + pull(controller) { | ||
| 22 | + if (i++ < n) controller.enqueue('a'); | ||
| 23 | + else controller.close(); | ||
| 24 | + }, | ||
| 25 | + }; | ||
| 26 | + | ||
| 27 | + const rs = new ReadableStream(source); | ||
| 28 | + const [branch1, branch2] = rs.tee(); | ||
| 29 | + const reader1 = branch1.getReader(); | ||
| 30 | + const reader2 = branch2.getReader(); | ||
| 31 | + let reads = 0; | ||
| 32 | + | ||
| 33 | + bench.start(); | ||
| 34 | + for (;;) { | ||
| 35 | + const [result1, result2] = await Promise.all([ | ||
| 36 | + reader1.read(), | ||
| 37 | + reader2.read(), | ||
| 38 | + ]); | ||
| 39 | + if (result1.done || result2.done) break; | ||
| 40 | + reads++; | ||
| 41 | + } | ||
| 42 | + bench.end(reads); | ||
| 43 | + console.assert(reads === n); | ||
| 44 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments