From 79025b6e2d620ed8e818418f6c2deaff093a86e1 Mon Sep 17 00:00:00 2001 From: Sam Thorogood Date: Tue, 28 Apr 2020 15:27:23 +1000 Subject: [PATCH] options --- bench/README.md | 4 ++-- bench/compare.js | 41 ++++++++++++++++++++++++++++------------- bench/package.json | 1 + bench/yarn.lock | 5 +++++ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/bench/README.md b/bench/README.md index 957b9cb..167a8b7 100644 --- a/bench/README.md +++ b/bench/README.md @@ -10,11 +10,11 @@ Usage: If you don't provide a source file, or specify a length instead, this will generate actual random text in JavaScript. For a better test, use suggested UTF-8 encoded source text from [Project Gutenberg](https://www.gutenberg.org/files/23841/23841-0.txt). -This has a ratio of "bytes-to-length" of 0.35; the lower the ratio, the better the test (ASCII has a ratio of 1). +This has a ratio of "bytes-to-length" of 0.35. This is an odd number, but we're comparing the on-disk UTF-8 bytes (which optimize for ASCII and other low Unicode values) to the length of JavaScript's UCS-2 / UTF-16 internal representation. All Unicode code points can be represented as either one or two "lengths" of a JavaScript string, but each code point can be between 1-4 bytes in UTF-8. -The possible ratios therefore range from 0.25 through 1.0. +The possible ratios therefore range from 0.25 (e.g., all emoji) through 1.0 (e.g., ASCII). # Results diff --git a/bench/compare.js b/bench/compare.js index 2407f4b..6f90910 100755 --- a/bench/compare.js +++ b/bench/compare.js @@ -3,12 +3,24 @@ const {performance} = require('perf_hooks'); const chalk = require('chalk'); const fs = require('fs'); +const mri = require('mri'); + +const options = mri(process.argv.slice(2), { + default: { + runs: 6, + native: false, + }, +}); const packages = ['fast-text-encoding', 'text-encoding', 'text-encoding-polyfill', 'text-encoding-utf-8', 'fastestsmallesttextencoderdecoder']; -const runs = 6; -const includeNative = true; -if (!includeNative) { +if (!options.native) { + global.Buffer.from = () => { + throw new Error('use of Buffer.from'); + }; + delete global.Buffer; + console.warn('NOT including any native code...'); + if (global.TextEncoder && global.TextDecoder) { global.TextEncoder.prototype.encode = () => { throw new Error('use of native encode()'); @@ -31,16 +43,15 @@ function buildRandomString(length) { } let string; -let buf = null; -if (+process.argv[2] || process.argv.length < 3) { +const firstArg = options._[0]; +if (firstArg === undefined || +firstArg) { // possibly a number - string = buildRandomString(+process.argv[2] || (256 * 256)); + string = buildRandomString(+firstArg || (256 * 256)); console.info(`compare (random): length=${chalk.yellow(string.length)}`); } else { - const stat = fs.statSync(process.argv[2]); - buf = fs.readFileSync(process.argv[2]); // no encoding, as Buffer - string = fs.readFileSync(process.argv[2], 'utf-8'); + const stat = fs.statSync(firstArg); + string = fs.readFileSync(firstArg, 'utf-8'); const ratio = (string.length / stat.size); console.info(`compare (file): length=${chalk.yellow(string.length)}, bytes=${chalk.yellow(stat.size)} (ratio=${chalk.yellow(ratio.toFixed(2))})`); } @@ -96,9 +107,13 @@ do { delete global.TextDecoder; delete global.TextEncoder; - packages.push('.local'); - require('../text.min.js'); - impl['.local'] = {TextEncoder: global.TextEncoder, TextDecoder: global.TextDecoder}; + try { + require('../text.min.js'); + packages.push('.local'); + impl['.local'] = {TextEncoder: global.TextEncoder, TextDecoder: global.TextDecoder}; + } catch (e) { + // ignore + } } while (false); delete global.TextDecoder; @@ -112,7 +127,7 @@ if (hasNative) { (async function() { - for (let i = 0; i < runs; ++i) { + for (let i = 0; i < options.runs; ++i) { shuffle(packages); console.info('run', (i + 1)); diff --git a/bench/package.json b/bench/package.json index 71d199e..7cda0d9 100644 --- a/bench/package.json +++ b/bench/package.json @@ -3,6 +3,7 @@ "chalk": "^4.0.0", "fast-text-encoding": "^1.0.2", "fastestsmallesttextencoderdecoder": "^1.0.21", + "mri": "^1.1.5", "text-encoding": "^0.7.0", "text-encoding-polyfill": "^0.6.7", "text-encoding-utf-8": "^1.0.2" diff --git a/bench/yarn.lock b/bench/yarn.lock index ee3d086..c86964c 100644 --- a/bench/yarn.lock +++ b/bench/yarn.lock @@ -50,6 +50,11 @@ has-flag@^4.0.0: resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ== +mri@^1.1.5: + version "1.1.5" + resolved "https://registry.yarnpkg.com/mri/-/mri-1.1.5.tgz#ce21dba2c69f74a9b7cf8a1ec62307e089e223e0" + integrity sha512-d2RKzMD4JNyHMbnbWnznPaa8vbdlq/4pNZ3IgdaGrVbBhebBsGUUE/6qorTMYNS6TwuH3ilfOlD2bf4Igh8CKg== + supports-color@^7.1.0: version "7.1.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"