Skip to content

Commit

Permalink
import bench suite
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Jul 10, 2018
1 parent a1afb07 commit db91220
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
99 changes: 99 additions & 0 deletions bench/colors.js
Original file line number Diff line number Diff line change
@@ -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`);
}
23 changes: 23 additions & 0 deletions bench/dryrun.js
Original file line number Diff line number Diff line change
@@ -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());
1 change: 1 addition & 0 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./colors');
15 changes: 15 additions & 0 deletions bench/load.js
Original file line number Diff line number Diff line change
@@ -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');
10 changes: 10 additions & 0 deletions bench/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit db91220

Please sign in to comment.