From db91220d846994aaa374f46abd90ee8a410f3ad7 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 10 Jul 2018 00:20:42 -0700 Subject: [PATCH] import bench suite --- bench/colors.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++ bench/dryrun.js | 23 +++++++++++ bench/index.js | 1 + bench/load.js | 15 +++++++ bench/package.json | 10 +++++ 5 files changed, 148 insertions(+) create mode 100755 bench/colors.js create mode 100755 bench/dryrun.js create mode 100755 bench/index.js create mode 100755 bench/load.js create mode 100755 bench/package.json diff --git a/bench/colors.js b/bench/colors.js new file mode 100755 index 0000000..cf59391 --- /dev/null +++ b/bench/colors.js @@ -0,0 +1,99 @@ +const { Suite } = require('benchmark'); +const cursor = require('ansi')(process.stdout); +const color = require('ansi-colors'); +const clorox = require('clorox'); +const chalk = require('chalk'); +const kleur = require('..'); + +const names = [ + 'reset', + 'bold', + 'dim', + 'italic', + 'underline', + 'inverse', + 'hidden', + 'strikethrough', + 'black', + 'red', + 'green', + 'yellow', + 'blue', + 'magenta', + 'cyan', + 'white', + 'gray', + 'bgBlack', + 'bgRed', + 'bgGreen', + 'bgYellow', + 'bgBlue', + 'bgMagenta', + 'bgCyan', + 'bgWhite' +]; + +const cycle = (e, nl) => { + cursor.eraseLine(); + cursor.horizontalAbsolute(); + cursor.write(' ' + e.target); + if (nl) cursor.write('\n'); +}; + +function bench(name) { + console.log(`\n# ${name}`); + const suite = new Suite(); + const res = { + run: suite.run.bind(suite), + add: (key, fn) => { + suite.add(key, { + onCycle: e => cycle(e), + onComplete: e => cycle(e, true), + fn: fn + }); + return res; + } + }; + return res; +} + +bench('All Colors') + .add('ansi-colors', () => { + names.forEach(name => color[name]('foo')); + }) + .add('chalk', () => { + names.forEach(name => chalk[name]('foo')); + }) + .add('clorox', () => { + names.forEach(name => clorox[name]('foo').toString()); + }) + .add('kleur', () => { + names.forEach(name => kleur[name]('foo')); + }) + .run(); + +bench('Stacked colors') + .add('ansi-colors', () => { + names.forEach(name => color[name].bold.underline.italic('foo')); + }) + .add('chalk', () => { + names.forEach(name => chalk[name].bold.underline.italic('foo')); + }) + .add('clorox', () => { + names.forEach(name => clorox[name].bold.underline.italic('foo').toString()); + }) + .add('kleur', () => { + names.forEach(name => kleur[name].bold.underline.italic('foo')); + }) + .run(); + +bench('Nested colors') + .add('ansi-colors', () => fixture(color)) + .add('chalk', () => fixture(chalk)) + .add('clorox', () => fixture(clorox).toString()) + .add('kleur', () => fixture(kleur)) + .run(); + +function fixture(lib) { + return lib.red(`a red ${lib.white('red')} red ${lib.red('red')} red ${lib.gray('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.blue('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')}red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')}red ${lib.green('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.magenta('red')} red ${lib.red('red')}red ${lib.red('red')} red ${lib.cyan('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.yellow('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} message`); +} diff --git a/bench/dryrun.js b/bench/dryrun.js new file mode 100755 index 0000000..266e9eb --- /dev/null +++ b/bench/dryrun.js @@ -0,0 +1,23 @@ +const libs = { + chalk: require('chalk'), + clorox: require('clorox'), + colors: require('ansi-colors'), + kleur: require('..') +}; + +const color = libs[process.argv[2] || 'kleur']; +[ + 'black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow', 'dim', 'gray', + 'bgBlack', 'bgBlue', 'bgCyan', 'bgGreen', 'bgMagenta', 'bgRed', 'bgWhite', 'bgYellow', + 'hidden', 'inverse', 'bold', 'italic', 'reset', 'strikethrough', 'underline' +].forEach(str => { + console.log(color[str]('~foobar~')); +}); + +console.log(); +console.log(color.bold(color.cyan('[info]')), color.cyan('This is some information')); +console.log(color.bold(color.yellow('[warning]')), color.yellow('This is a warning')); +console.log(color.bold(color.red('[ERROR]')), color.red('Danger! There was an error!')); +console.log(); + +console.log(color.red(`a red ${color.white('white')} red ${color.red('red')} red ${color.gray('gray')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.blue('blue')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')}red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')}red ${color.green('green')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.magenta('red')} red ${color.red('red')}red ${color.red('red')} red ${color.cyan('cyan')} red ${color.red('red')} red ${color.red('red')} red ${color.yellow('yellow')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.blue('blue')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} message`).toString()); diff --git a/bench/index.js b/bench/index.js new file mode 100755 index 0000000..5ad2283 --- /dev/null +++ b/bench/index.js @@ -0,0 +1 @@ +module.exports = require('./colors'); diff --git a/bench/load.js b/bench/load.js new file mode 100755 index 0000000..2e7719b --- /dev/null +++ b/bench/load.js @@ -0,0 +1,15 @@ +console.time('ansi-colors'); +const color = require('ansi-colors'); +console.timeEnd('ansi-colors'); + +console.time('chalk'); +const chalk = require('chalk'); +console.timeEnd('chalk'); + +console.time('clorox'); +const clorox = require('clorox'); +console.timeEnd('clorox'); + +console.time('kleur'); +const kleur = require('..'); +console.timeEnd('kleur'); diff --git a/bench/package.json b/bench/package.json new file mode 100755 index 0000000..9b036b6 --- /dev/null +++ b/bench/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "devDependencies": { + "ansi": "^0.3.1", + "ansi-colors": "^2.0.2", + "benchmark": "^2.1.4", + "chalk": "^2.4.0", + "clorox": "^1.0.3" + } +}