Skip to content

Commit

Permalink
Create byteLength.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Jan 1, 2025
1 parent 57480d7 commit 7da30c1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bench/snippets/byteLength.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Buffer } from "node:buffer";
import { bench, run } from "../runner.mjs";

const variations = [
["latin1", "hello world"],
["utf16", "hello emoji 🤔"],
];

for (const [label, string] of variations) {
const big = Buffer.alloc(1000000, string).toString();
const small = Buffer.from(string).toString();
const substring = big.slice(0, big.length - 2);

bench(`${substring.length}`, () => {
return Buffer.byteLength(substring, "utf8");
});

bench(`${small.length}`, () => {
return Buffer.byteLength(small);
});

bench(`${big.length}`, () => {
return Buffer.byteLength(big);
});
}

await run();

0 comments on commit 7da30c1

Please sign in to comment.