-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathcount-utf8.ts
More file actions
58 lines (47 loc) · 1.76 KB
/
count-utf8.ts
File metadata and controls
58 lines (47 loc) · 1.76 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* eslint-disable no-console */
import { utf8CountJs, WASM_AVAILABLE } from "../src/utils/utf8.ts";
import { getWasmError, utf8CountWasm, RAB_AVAILABLE } from "../src/utils/utf8-wasm.ts";
// @ts-ignore
import Benchmark from "benchmark";
// description
console.log("utf8CountJs - pure JS implementation");
console.log("utf8CountWasm - WebAssembly implementation");
// Show wasm status
console.log("=".repeat(60));
console.log("WebAssembly Status:");
console.log(` WASM_AVAILABLE: ${WASM_AVAILABLE}`);
if (WASM_AVAILABLE) {
console.log(" js-string-builtins: enabled");
console.log(` RAB_AVAILABLE: ${RAB_AVAILABLE} (resizable ArrayBuffer integration)`);
} else {
const error = getWasmError();
console.log(` Error: ${error?.message || "unknown"}`);
if (error?.message?.includes("js-string") || error?.message?.includes("builtin")) {
console.log("\n js-string-builtins is enabled by default in Node.js 24+ (V8 13.6+).");
console.log(" For older versions, run with:");
console.log(" node --experimental-wasm-imported-strings node_modules/.bin/ts-node benchmark/count-utf8.ts");
}
}
console.log("=".repeat(60));
for (const baseStr of ["A", "あ", "🌏"]) {
const dataSet = [10, 30, 50, 100, 200, 500, 1000].map((n) => {
return baseStr.repeat(n);
});
for (const str of dataSet) {
const byteLength = utf8CountJs(str);
console.log(`\n## string "${baseStr}" (strLength=${str.length}, byteLength=${byteLength})\n`);
const suite = new Benchmark.Suite();
suite.add("utf8CountJs", () => {
utf8CountJs(str);
});
if (WASM_AVAILABLE) {
suite.add("utf8CountWasm", () => {
utf8CountWasm(str);
});
}
suite.on("cycle", (event: any) => {
console.log(String(event.target));
});
suite.run();
}
}