-
-
Save Jarred-Sumner/90db1a06c5e7b1c1ab55b5581c62a86d to your computer and use it in GitHub Desktop.
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
import { file } from "bun"; | |
import { bench, run } from "mitata"; | |
const SIZE = 1024; | |
const MEDIUM_SIZE = 1024 * 16; | |
const LARGE_SIZE = 1024 * 128; | |
const TOTAL = 1024 * 1024 * 2; | |
const filename = `Math.random (2 MB).blob`; | |
// bench(`stream 1,024 MB file (16 KB chunks)`, async () => { | |
// var readable = file(filename).stream( | |
// // this optional param sets the maximum chunk size | |
// SIZE | |
// ); | |
// var reader = readable.getReader({ mode: "byob" }); | |
// var total = 0; | |
// const view = allocUnsafe(SIZE); | |
// while (true) { | |
// var { value, done } = await reader.read(view); | |
// if (done) break; | |
// total += value.byteLength; | |
// if (value.byteLength !== SIZE) throw new Error("invalid size"); | |
// } | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
// bench(`stream 2 MB file (1 KB chunks)`, async () => { | |
// var readable = file(filename).stream( | |
// // this optional param sets the maximum chunk size | |
// SIZE | |
// ); | |
// var reader = readable.getReader(); | |
// var total = 0; | |
// while (true) { | |
// var { value, done } = await reader.read(); | |
// if (done) break; | |
// total += value.byteLength; | |
// if (value.byteLength !== SIZE) throw new Error("invalid size"); | |
// } | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
// bench(`stream 2 MB file (16 KB chunks)`, async () => { | |
// var readable = file(filename).stream( | |
// // this optional param sets the maximum chunk size | |
// MEDIUM_SIZE | |
// ); | |
// var reader = readable.getReader(); | |
// var total = 0; | |
// while (true) { | |
// var { value, done } = await reader.read(); | |
// if (done) break; | |
// total += value.byteLength; | |
// if (value.byteLength !== MEDIUM_SIZE) throw new Error("invalid size"); | |
// } | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
bench(`stream 2 MB file (128 KB chunks)`, async () => { | |
var readable = file(filename).stream( | |
// this optional param sets the maximum chunk size | |
LARGE_SIZE | |
); | |
var reader = readable.getReader(); | |
var total = 0; | |
while (true) { | |
var { value, done } = await reader.read(); | |
if (done) break; | |
total += value.byteLength; | |
if (value.byteLength !== LARGE_SIZE) throw new Error("invalid size"); | |
} | |
if (total !== TOTAL) throw new Error("invalid total"); | |
}); | |
// bench(`stream 1,024 MB file (128 MB chunks)`, async () => { | |
// var readable = file(filename).stream( | |
// // this optional param sets the maximum chunk size | |
// LARGE_SIZE | |
// ); | |
// var reader = readable.getReader({ mode: "byob" }); | |
// var total = 0; | |
// const view = allocUnsafe(LARGE_SIZE); | |
// while (true) { | |
// var { value, done } = await reader.read(view); | |
// if (done) break; | |
// console.log(value.byteLength, total, total - value.byteLength); | |
// total += value.byteLength; | |
// if (value.byteLength !== LARGE_SIZE) throw new Error("invalid size"); | |
// } | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
await run(); |
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
import { bench, run } from "https://esm.run/mitata"; | |
import { readableStreamFromReader } from "https://deno.land/std@0.141.0/streams/mod.ts"; | |
const SIZE = 1024; | |
const MEDIUM_SIZE = 1024 * 16; | |
const LARGE_SIZE = 1024 * 128; | |
const TOTAL = 1024 * 1024 * 2; | |
const filename = `Math.random (2 MB).blob`; | |
// bench(`stream 2 MB file (1 KB chunks)`, async () => { | |
// var total = 0; | |
// var file = await Deno.open(filename, { | |
// read: true, | |
// }); | |
// var reader = readableStreamFromReader(file, { | |
// chunkSize: SIZE, | |
// }).getReader(); | |
// var total = 0; | |
// while (true) { | |
// var { value, done } = await reader.read(); | |
// if (done) break; | |
// total += value.byteLength; | |
// if (value.byteLength !== SIZE) throw new Error("invalid size"); | |
// } | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
// bench(`stream 2 MB file (16 KB chunks)`, async () => { | |
// var total = 0; | |
// var file = await Deno.open(filename, { | |
// read: true, | |
// }); | |
// var reader = readableStreamFromReader(file, { | |
// chunkSize: MEDIUM_SIZE, | |
// }).getReader(); | |
// var total = 0; | |
// while (true) { | |
// var { value, done } = await reader.read(); | |
// if (done) break; | |
// total += value.byteLength; | |
// if (value.byteLength !== MEDIUM_SIZE) throw new Error("invalid size"); | |
// } | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
bench(`stream 2 MB file (128 KB chunks)`, async () => { | |
var total = 0; | |
var file = await Deno.open(filename, { | |
read: true, | |
}); | |
var reader = readableStreamFromReader(file, { | |
chunkSize: LARGE_SIZE, | |
}).getReader(); | |
var total = 0; | |
while (true) { | |
var { value, done } = await reader.read(); | |
if (done) break; | |
total += value.byteLength; | |
if (value.byteLength !== LARGE_SIZE) throw new Error("invalid size"); | |
} | |
if (total !== TOTAL) throw new Error("invalid total"); | |
}); | |
// bench(`stream 2 MB file (128 MB chunks)`, async () => { | |
// var total = 0; | |
// var file = await Deno.open(filename, { | |
// read: true, | |
// }); | |
// const iter = Deno.iter(file, { | |
// bufSize: LARGE_SIZE, | |
// }); | |
// for await (const chunk of iter) { | |
// if (chunk.byteLength !== LARGE_SIZE) throw new Error("invalid size"); | |
// total += chunk.byteLength; | |
// } | |
// file.close(); | |
// if (total !== TOTAL) throw new Error("invalid total"); | |
// }); | |
await run(); |
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
import { bench, run } from "mitata"; | |
import { createReadStream } from "node:fs"; | |
const SIZE = 1024; | |
const MEDIUM_SIZE = 1024 * 16; | |
const LARGE_SIZE = 1024 * 128; | |
const TOTAL = 1024 * 1024 * 2; | |
const filename = `Math.random (2 MB).blob`; | |
// bench(`stream 2 MB file (1 KB chunks)`, async () => { | |
// return new Promise((resolve, reject) => { | |
// var stream = createReadStream(filename, { | |
// highWaterMark: SIZE, | |
// }); | |
// var total = 0; | |
// stream.on("open", () => { | |
// stream.on("data", (chunk) => { | |
// total += chunk.byteLength; | |
// if (chunk.byteLength !== SIZE) throw new Error("invalid size"); | |
// }); | |
// stream.on("end", () => { | |
// if (total !== TOTAL) reject(); | |
// else resolve(); | |
// }); | |
// }); | |
// }); | |
// }); | |
// bench(`stream 2 MB file (16 KB chunks)`, async () => { | |
// return new Promise((resolve, reject) => { | |
// var stream = createReadStream(filename, { | |
// highWaterMark: MEDIUM_SIZE, | |
// }); | |
// var total = 0; | |
// stream.on("open", () => { | |
// stream.on("data", (chunk) => { | |
// total += chunk.byteLength; | |
// if (chunk.byteLength !== MEDIUM_SIZE) throw new Error("invalid size"); | |
// }); | |
// stream.on("end", () => { | |
// if (total !== TOTAL) reject(); | |
// else resolve(); | |
// }); | |
// }); | |
// }); | |
// }); | |
bench(`stream 2 MB file (128 KB chunks)`, async () => { | |
return new Promise((resolve, reject) => { | |
var stream = createReadStream(filename, { | |
highWaterMark: LARGE_SIZE, | |
}); | |
var total = 0; | |
stream.on("open", () => { | |
stream.on("data", (chunk) => { | |
total += chunk.byteLength; | |
if (chunk.byteLength !== LARGE_SIZE) throw new Error("invalid size"); | |
}); | |
stream.on("end", () => { | |
if (total !== TOTAL) reject(); | |
else resolve(); | |
}); | |
}); | |
}); | |
}); | |
await run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment