aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets/buffer-to-string.mjs
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-08-21 08:31:17 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-21 08:31:17 -0700
commitdef5a85d90e5102ab52e6960e8caab3c3f8ab3e8 (patch)
treeb8501bb40eb97cc019eea5f83454f8fc1c95c2e7 /bench/snippets/buffer-to-string.mjs
parent1b8f5697929812d4c7abd0edff8ad1e8f12b4e14 (diff)
downloadbun-def5a85d90e5102ab52e6960e8caab3c3f8ab3e8.tar.gz
bun-def5a85d90e5102ab52e6960e8caab3c3f8ab3e8.tar.zst
bun-def5a85d90e5102ab52e6960e8caab3c3f8ab3e8.zip
40x faster .toString('hex') (#4237)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'bench/snippets/buffer-to-string.mjs')
-rw-r--r--bench/snippets/buffer-to-string.mjs29
1 files changed, 29 insertions, 0 deletions
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();