Skip to content

Commit

Permalink
include local code
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Apr 28, 2020
1 parent f8a589e commit 13b4e90
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions bench/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ const chalk = require('chalk');
const fs = require('fs');

const packages = ['fast-text-encoding', 'text-encoding', 'text-encoding-polyfill', 'text-encoding-utf-8', 'fastestsmallesttextencoderdecoder'];
const runs = 8;
const runs = 6;
const includeNative = true;

if (!includeNative) {
if (global.TextEncoder && global.TextDecoder) {
global.TextEncoder.prototype.encode = () => {
throw new Error('use of native encode()');
};
global.TextDecoder.prototype.decode = () => {
throw new Error('use of native decode()');
};
}
delete global.TextEncoder;
delete global.TextDecoder;
}

function buildRandomString(length) {
const parts = [];
Expand All @@ -17,13 +31,15 @@ function buildRandomString(length) {
}

let string;
let buf = null;

if (+process.argv[2] || process.argv.length < 3) {
// possibly a number
string = buildRandomString(+process.argv[2] || (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 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 All @@ -32,7 +48,9 @@ if (+process.argv[2] || process.argv.length < 3) {
// remove 'text-encoding-utf-8' after a certain size as it's just pathologically bad
if (string.length >= 32768) {
const index = packages.indexOf('text-encoding-utf-8');
packages.splice(index, 1);
if (index !== -1) {
packages.splice(index, 1);
}
}

console.info('');
Expand Down Expand Up @@ -74,6 +92,19 @@ for (const name of packages) {
impl[name] = use;
}

do {
delete global.TextDecoder;
delete global.TextEncoder;

packages.push('.local');
require('../text.min.js');
impl['.local'] = {TextEncoder: global.TextEncoder, TextDecoder: global.TextDecoder};
} while (false);

delete global.TextDecoder;
delete global.TextEncoder;


if (hasNative) {
packages.push('.native');
impl['.native'] = nativeImpl;
Expand All @@ -86,9 +117,6 @@ if (hasNative) {
console.info('run', (i + 1));

for (const name of packages) {
delete global.TextDecoder;
delete global.TextEncoder;

console.debug(chalk.gray(name));
const use = impl[name];

Expand Down

0 comments on commit 13b4e90

Please sign in to comment.