aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets/buffer-to-string.mjs
blob: f4a1695725919c6db76491bb8b3bd8929379bd43 (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
29
import { bench, run } from "./runner.mjs";
import { Buffer } from "node:buffer";
import crypto from "node:crypto";

const bigBuffer = Buffer.from("hello world".repeat(10000));
const converted = bigBuffer.toString("base64");
const uuid = crypto.randomBytes(16);

bench(`Buffer(${bigBuffer.byteLength}).toString('base64')`, () => {
  return bigBuffer.toString("base64");
});

bench(`Buffer(${uuid.byteLength}).toString('base64')`, () => {
  return uuid.toString("base64");
});

bench(`Buffer(${bigBuffer.byteLength}).toString('hex')`, () => {
  return bigBuffer.toString("hex");
});

bench(`Buffer(${uuid.byteLength}).toString('hex')`, () => {
  return uuid.toString("hex");
});

bench(`Buffer(${bigBuffer.byteLength}).toString('ascii')`, () => {
  return bigBuffer.toString("ascii");
});

await run();