Update text-encoder-stream.mjs

This commit is contained in:
Jarred Sumner
2024-08-10 03:15:11 -07:00
parent d861347dc5
commit 5f08478229

View File

@@ -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();