From def5a85d90e5102ab52e6960e8caab3c3f8ab3e8 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 21 Aug 2023 08:31:17 -0700 Subject: 40x faster .toString('hex') (#4237) Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- bench/snippets/buffer-to-string.mjs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 bench/snippets/buffer-to-string.mjs (limited to 'bench/snippets/buffer-to-string.mjs') diff --git a/bench/snippets/buffer-to-string.mjs b/bench/snippets/buffer-to-string.mjs new file mode 100644 index 000000000..f4a169572 --- /dev/null +++ b/bench/snippets/buffer-to-string.mjs @@ -0,0 +1,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(); -- cgit v1.2.3