unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

bench/xlang/node/main.cjs
1// Node peer of bench/xlang/moonbit: the same cases on the same inputs, timed2// with the same protocol and printed in the same row format. It measures what3// a JavaScript program gets from the platform: String.prototype.normalize and4// Intl.Segmenter (ICU4C behind V8), url.domainTo* (ada) and the bundled5// punycode.js. Every call crosses from JavaScript into the runtime.6'use strict';78const fs = require('node:fs');9const url = require('node:url');10const punycode = require('node:punycode');1112let sink = 0;13function run(iterations, f) {14  const start = process.hrtime.bigint();15  for (let i = 0; i < iterations; i++) sink ^= f();16  return Number(process.hrtime.bigint() - start);17}1819// The latest result a case built. Every result is stored here, where it can be20// read later, so that V8 cannot drop the work that builds it: it drops a string21// that only has its length read, as it can compute the length without it.22let kept;23function keep(result) {24  kept = result;25  return result.length;26}2728// Double the iteration count until one sample takes at least 5 ms, then print29// the fastest of 5 samples, the first of which is that one, as30// library<TAB>case<TAB>ns<TAB>units<TAB>check.31// The library column is empty: each case has a single platform API.32function measure(name, units, check, f) {33  let iterations = 1;34  let best = run(iterations, f);35  while (best < 5e6) {36    iterations *= 2;37    best = run(iterations, f);38  }39  for (let i = 1; i < 5; i++) best = Math.min(best, run(iterations, f));40  console.log(`\t${name}\t${best / iterations}\t${units}\t${check}`);41}4243const read = (path) => fs.readFileSync(path, 'utf8');44// Input names in `directory`: the .txt files without their extension.45const names = (directory) =>46  fs.readdirSync(directory).filter((file) => file.endsWith('.txt')).map((file) => file.slice(0, -4)).sort();47// The name<TAB>value lines of a .tsv input.48const pairs = (path) => read(path).split('\n').filter((line) => line.includes('\t')).map((line) => line.split('\t'));49const codePoints = (text) => {50  let count = 0;51  for (const _ of text) count++;52  return count;53};5455function benchNormalization(inputs) {56  for (const name of names(`${inputs}/norm`)) {57    const original = read(`${inputs}/norm/${name}.txt`);58    const decomposed = read(`${inputs}/normd/${name}.txt`);59    const units = codePoints(original);60    for (const form of ['NFC', 'NFD', 'NFKC', 'NFKD']) {61      measure(`norm/${form.toLowerCase()}/${name}`, units, codePoints(original.normalize(form)),62        () => keep(original.normalize(form)));63    }64    measure(`norm/nfd>nfc/${name}`, codePoints(decomposed), codePoints(decomposed.normalize('NFC')),65      () => keep(decomposed.normalize('NFC')));66  }67}6869function benchSegmentation(inputs) {70  const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });71  for (const name of names(`${inputs}/seg`)) {72    const text = read(`${inputs}/seg/${name}.txt`);73    const count = () => {74      let clusters = 0;75      for (const _ of segmenter.segment(text)) clusters++;76      return clusters;77    };78    measure(`seg/graphemes/${name}`, codePoints(text), count(), count);79  }80}8182function benchIdna(inputs) {83  for (const [name, domain] of pairs(`${inputs}/idna.tsv`)) {84    measure(`idna/to_ascii/${name}`, 1, codePoints(url.domainToASCII(domain)),85      () => keep(url.domainToASCII(domain)));86    measure(`idna/to_unicode/${name}`, 1, codePoints(url.domainToUnicode(domain)),87      () => keep(url.domainToUnicode(domain)));88  }89}9091function benchPunycode(inputs) {92  for (const [name, input] of pairs(`${inputs}/puny.tsv`)) {93    const encoded = punycode.encode(input);94    measure(`puny/encode/${name}`, 1, encoded.length, () => keep(punycode.encode(input)));95    measure(`puny/decode/${name}`, 1, codePoints(input), () => keep(punycode.decode(encoded)));96  }97}9899const [inputs, group] = process.argv.slice(2);100if (!inputs) {101  console.error('usage: node main.cjs <inputs> [group]');102  process.exit(2);103}104const groups = { norm: benchNormalization, seg: benchSegmentation, idna: benchIdna, puny: benchPunycode };105for (const [name, bench] of Object.entries(groups)) {106  if (!group || group === name) bench(inputs);107}108