Skip to content

Commit 7da30c1

Browse files
committed
Create byteLength.mjs
1 parent 57480d7 commit 7da30c1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

bench/snippets/byteLength.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Buffer } from "node:buffer";
2+
import { bench, run } from "../runner.mjs";
3+
4+
const variations = [
5+
["latin1", "hello world"],
6+
["utf16", "hello emoji 🤔"],
7+
];
8+
9+
for (const [label, string] of variations) {
10+
const big = Buffer.alloc(1000000, string).toString();
11+
const small = Buffer.from(string).toString();
12+
const substring = big.slice(0, big.length - 2);
13+
14+
bench(`${substring.length}`, () => {
15+
return Buffer.byteLength(substring, "utf8");
16+
});
17+
18+
bench(`${small.length}`, () => {
19+
return Buffer.byteLength(small);
20+
});
21+
22+
bench(`${big.length}`, () => {
23+
return Buffer.byteLength(big);
24+
});
25+
}
26+
27+
await run();

0 commit comments

Comments
 (0)