aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets/write.node.mjs
blob: f59c98aefa96f2c571e935e83aa10771dd3a64cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// @runtime node, bun, deno
import { bench, run } from "./runner.mjs";
import { Buffer } from "node:buffer";
import { openSync } from "node:fs";
import { writeFile } from "node:fs/promises";
import { writeSync as write } from "node:fs";

bench("writeFile(/tmp/foo.txt, short string)", async () => {
  await writeFile("/tmp/foo.txt", "short string", "utf8");
});

bench("writeFile(/tmp/foo.txt, Buffer.from(short string))", async () => {
  await writeFile("/tmp/foo.txt", Buffer.from("short string"));
});

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.from("short string"));
  if (bytesWritten !== 12) throw new Error("wrote !== 12");
});

await run();