Skip to content

Instantly share code, notes, and snippets.

@gfx
Created July 24, 2019 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfx/e3e33c80848f734a81dbd030fca16230 to your computer and use it in GitHub Desktop.
Save gfx/e3e33c80848f734a81dbd030fca16230 to your computer and use it in GitHub Desktop.
// NOTE: use the last segment of the result CSV
/* eslint-disable no-console */
import { utf8EncodeJs, utf8Count, utf8DecodeJs, utf8DecodeTD } from "../src/utils/utf8";
import { utf8DecodeWasm } from "../src/wasmFunctions";
// @ts-ignore
import Benchmark from "benchmark";
for (const baseStr of ["A"]) {
const table = {
"": [],
} as Record<string, Array<string | number>>;
const dataSet = [10, 100, 200, 500, 1_000, 10_000].map((n) => {
table[""].push(n);
return baseStr.repeat(n);
});
for (const str of dataSet) {
const byteLength = utf8Count(str);
const bytes = new Uint8Array(new ArrayBuffer(byteLength));
utf8EncodeJs(str, bytes, 0);
console.log(`\n## string "${baseStr}" x ${str.length} (byteLength=${byteLength})\n`);
const suite = new Benchmark.Suite();
suite.add("utf8DecodeJs", () => {
if (utf8DecodeJs(bytes, 0, byteLength) !== str) {
throw new Error("wrong result!");
}
});
suite.add("utf8DecodeWasm", () => {
if (utf8DecodeWasm(bytes, 0, byteLength) !== str) {
throw new Error("wrong result!");
}
});
suite.add("TextDecoder", () => {
if (utf8DecodeTD(bytes, 0, byteLength) !== str) {
throw new Error("wrong result!");
}
});
suite.on("cycle", (event: any) => {
if (!table[event.target.name]) {
table[event.target.name] = [];
}
table[event.target.name].push(Math.log10(event.target.hz));
console.log(String(event.target));
});
suite.on("complete", () => {
console.log();
// dump as CSV
for (const name of Object.keys(table)) {
const row = [name, ...table[name]].join(",");
console.log(row);
}
});
suite.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment