Skip to content

Commit

Permalink
options
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Apr 28, 2020
1 parent 320a34d commit 79025b6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 28 additions & 13 deletions bench/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()');
Expand All @@ -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))})`);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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));

Expand Down
1 change: 1 addition & 0 deletions bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions bench/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 79025b6

Please sign in to comment.