From 8af05c28a3dea66df24a7e818a7d5c9ecd4ddc5a Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 7 Sep 2022 21:20:41 -0700 Subject: Add benchmark for small write() performance --- bench/snippets/write.node.mjs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 bench/snippets/write.node.mjs (limited to 'bench/snippets/write.node.mjs') diff --git a/bench/snippets/write.node.mjs b/bench/snippets/write.node.mjs new file mode 100644 index 000000000..bafaceecb --- /dev/null +++ b/bench/snippets/write.node.mjs @@ -0,0 +1,27 @@ +import { bench, run } from "mitata"; +import { openSync } from "fs"; +import { writeFile } from "fs/promises"; +import { writeSync as write } from "fs"; + +bench("writeFile(/tmp/foo.txt, short string)", async () => { + await writeFile("/tmp/foo.txt", "short string", "utf8"); +}); + +const buffer = Buffer.from("short string"); +bench("writeFile(/tmp/foo.txt, Buffer.from(short string))", async () => { + await writeFile("/tmp/foo.txt", buffer); +}); + +const fd = openSync("/tmp/foo.txt", "w"); + +bench("write(fd, short string)", () => { + const bytesWritten = write(fd, "short string", "utf8"); + if (bytesWritten !== 12) throw new Error("wrote !== 12"); +}); + +bench("write(fd, Uint8Array(short string))", () => { + const bytesWritten = write(fd, buffer); + if (bytesWritten !== 12) throw new Error("wrote !== 12"); +}); + +await run(); -- cgit v1.2.3