diff options
-rw-r--r-- | bench/snippets/write.bun.js | 24 | ||||
-rw-r--r-- | bench/snippets/write.node.mjs | 27 |
2 files changed, 51 insertions, 0 deletions
diff --git a/bench/snippets/write.bun.js b/bench/snippets/write.bun.js new file mode 100644 index 000000000..190604a44 --- /dev/null +++ b/bench/snippets/write.bun.js @@ -0,0 +1,24 @@ +import { bench, run } from "mitata"; +import { write } from "bun"; +import { openSync } from "fs"; + +bench('write(/tmp/foo.txt, "short string")', async () => { + await write("/tmp/foo.txt", "short string"); +}); + +const buffer = Buffer.from("short string"); +bench('write(/tmp/foo.txt, Buffer.from("short string"))', async () => { + await write("/tmp/foo.txt", buffer); +}); + +const fd = openSync("/tmp/foo.txt", "w"); + +bench('write(fd, "short string")', async () => { + await write(fd, "short string"); +}); + +bench('write(fd, Buffer.from("short string"))', async () => { + await write(fd, buffer); +}); + +await run(); 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(); |