From 5f08478229552e050d928db01b22d19cc4ecd9c8 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sat, 10 Aug 2024 03:15:11 -0700 Subject: [PATCH] Update text-encoder-stream.mjs --- bench/snippets/text-encoder-stream.mjs | 46 ++++++++++---------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/bench/snippets/text-encoder-stream.mjs b/bench/snippets/text-encoder-stream.mjs index 8fe4ed621b..856f2719a8 100644 --- a/bench/snippets/text-encoder-stream.mjs +++ b/bench/snippets/text-encoder-stream.mjs @@ -1,23 +1,18 @@ import { bench, run } from "./runner.mjs"; -const latin1 = `hello hello hello!!!!`.repeat(102400).split("").join(""); +const latin1 = `hello hello hello!!!! `.repeat(10240); function create(src) { function split(str, chunkSize) { let chunkedHTML = []; let html = str; while (html.length > 0) { - chunkedHTML.push(html.slice(0, chunkSize).split("").join("")); + chunkedHTML.push(html.slice(0, chunkSize)); html = html.slice(chunkSize); } return chunkedHTML; } - const quarterKB = split(src, 256); - const oneKB = split(src, 1024); - const fourKB = split(src, 4096); - const sixteenKB = split(src, 16 * 1024); - async function runBench(chunks) { const encoderStream = new TextEncoderStream(); const stream = new ReadableStream({ @@ -28,34 +23,27 @@ function create(src) { controller.close(); }, }).pipeThrough(encoderStream); - for (let reader = stream.getReader(); ; ) { - const { value, done } = await reader.read(); - if (done) break; - } + return await new Response(stream).text(); } // if (new TextDecoder().decode(await runBench(oneKB)) !== src) { // throw new Error("Benchmark failed"); // } - - bench(`${(src.length / 1024) | 0} KB of HTML in 0.25 KB chunks`, async () => { - await runBench(quarterKB); - }); - - bench(`${(src.length / 1024) | 0} KB of HTML in 1 KB chunks`, async () => { - await runBench(oneKB); - }); - - bench(`${(src.length / 1024) | 0} KB of HTML in 4 KB chunks`, async () => { - await runBench(fourKB); - }); - - bench(`${(src.length / 1024) | 0} KB of HTML in 16 KB chunks`, async () => { - await runBench(sixteenKB); - }); + const sizes = [1024, 16 * 1024, 64 * 1024, 256 * 1024]; + for (const chunkSize of sizes) { + const text = split(src, chunkSize); + bench( + `${Math.round(src.length / 1024)} KB of text in ${Math.round(chunkSize / 1024) > 0 ? Math.round(chunkSize / 1024) : (chunkSize / 1024).toFixed(2)} KB chunks`, + async () => { + await runBench(text); + }, + ); + } } - create(latin1); -create(await fetch("https://bun.sh").then(res => res.text())); +create( + // bun's old readme was extremely long + await fetch("https://web.archive.org/web/20230119110956/https://github.com/oven-sh/bun").then(res => res.text()), +); await run();