| 1 | import { Effect, Stream } from "effect" |
| 2 | import { Bench } from "tinybench" |
| 3 | |
| 4 | const bench = new Bench() |
| 5 | |
| 6 | // ~100 short lines, all in one chunk |
| 7 | const singleChunk = Array.from({ length: 100 }, (_, i) => `line ${i}`).join("\n") + "\n" |
| 8 | |
| 9 | // same content split into many small chunks (simulates streaming I/O) |
| 10 | const manyChunks: Array<string> = [] |
| 11 | for (let i = 0; i < singleChunk.length; i += 8) { |
| 12 | manyChunks.push(singleChunk.substring(i, i + 8)) |
| 13 | } |
| 14 | |
| 15 | // mixed line endings |
| 16 | const mixedEndings = "alpha\r\nbravo\rcharlie\ndelta\r\necho\rfoxtrot\n" |
| 17 | const mixedChunks: Array<string> = [] |
| 18 | for (let i = 0; i < mixedEndings.length; i += 5) { |
| 19 | mixedChunks.push(mixedEndings.substring(i, i + 5)) |
| 20 | } |
| 21 | |
| 22 | const run = (chunks: Array<string>) => |
| 23 | Effect.runPromise( |
| 24 | Stream.fromIterable(chunks).pipe( |
| 25 | Stream.splitLines, |
| 26 | Stream.runCollect |
| 27 | ) |
| 28 | ) |
| 29 | |
| 30 | bench |
| 31 | .add("single chunk (100 lines)", async function() { |
| 32 | await run([singleChunk]) |
| 33 | }) |
| 34 | .add("many small chunks (100 lines)", async function() { |
| 35 | await run(manyChunks) |
| 36 | }) |
| 37 | .add("mixed line endings (single chunk)", async function() { |
| 38 | await run([mixedEndings]) |
| 39 | }) |
| 40 | .add("mixed line endings (small chunks)", async function() { |
| 41 | await run(mixedChunks) |
| 42 | }) |
| 43 | |
| 44 | await bench.run() |
| 45 | |
| 46 | console.table(bench.table()) |