-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathencode-string.ts
More file actions
64 lines (52 loc) · 1.98 KB
/
encode-string.ts
File metadata and controls
64 lines (52 loc) · 1.98 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
59
60
61
62
63
64
/* eslint-disable no-console */
import { utf8EncodeJs, utf8Count, utf8EncodeTE, WASM_AVAILABLE } from "../src/utils/utf8.ts";
import { getWasmError, utf8EncodeWasm, RAB_AVAILABLE } from "../src/utils/utf8-wasm.ts";
// @ts-ignore
import Benchmark from "benchmark";
// description
console.log("utf8EncodeJs - pure JS implementation");
console.log("utf8EncodeTE - TextEncoder implementation");
console.log("utf8EncodeWasm - 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/encode-string.ts");
}
}
console.log("=".repeat(60));
for (const baseStr of ["A", "あ", "🌏"]) {
const dataSet = [10, 30, 50, 100].map((n) => {
return baseStr.repeat(n);
});
for (const str of dataSet) {
const byteLength = utf8Count(str);
const buffer = new Uint8Array(byteLength);
console.log(`\n## string "${baseStr}" (strLength=${str.length}, byteLength=${byteLength})\n`);
const suite = new Benchmark.Suite();
suite.add("utf8EncodeJs", () => {
utf8EncodeJs(str, buffer, 0);
});
suite.add("utf8EncodeTE", () => {
utf8EncodeTE(str, buffer, 0);
});
if (WASM_AVAILABLE) {
suite.add("utf8EncodeWasm", () => {
utf8EncodeWasm(str, buffer, 0);
});
}
suite.on("cycle", (event: any) => {
console.log(String(event.target));
});
suite.run();
}
}