Skip to content

Commit 5cb30a0

Browse files
committed
chore: refactor bench
1 parent 965d71c commit 5cb30a0

File tree

5 files changed

+46
-67
lines changed

5 files changed

+46
-67
lines changed

bench/index.js

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
'use strict';
2727

28-
import Bench from './lib/bench.js';
29-
3028
// vendor libraries
3129
import chalk from 'chalk';
3230
import colorsJs from '@colors/colors';
@@ -37,12 +35,11 @@ import colorCli from 'colors-cli/safe.js';
3735
import kleur from 'kleur';
3836
import * as kolorist from 'kolorist';
3937
import picocolors from 'picocolors';
40-
import { Ansis, cyan, red, yellow, hex, rgb } from 'ansis';
41-
42-
import spectrum from '../examples/fixtures/spectrum.js';
43-
import { colorLevels, LEVEL_TRUECOLOR } from '../src/color-levels.js';
38+
import { Ansis, cyan, red, yellow, hex } from 'ansis';
4439

40+
import Bench from './lib/bench.js';
4541
import packages from './packages.js';
42+
import { colorLevels, LEVEL_256COLORS } from '../src/color-levels.js';
4643

4744
// create a new instance of Ansis for correct measure in benchmark
4845
const ansis = new Ansis();
@@ -53,8 +50,8 @@ const log = console.log;
5350
log();
5451
log(cyan.inverse` Colors `, `Your terminal supports ${cyan(colorLevels[colorLevel])}.`);
5552

56-
if (colorLevel < LEVEL_TRUECOLOR) {
57-
log(red.inverse` WARNING `, yellow`Your terminal doesn't support Truecolor!`);
53+
if (colorLevel < LEVEL_256COLORS) {
54+
log(red.inverse` WARNING `, yellow`Your terminal doesn't support 256 or Truecolor!`);
5855
log('The result of some tests can be NOT correct!\nChoose a modern terminal, e.g. iTerm.\n');
5956
}
6057

@@ -261,43 +258,7 @@ bench('New Line').
261258
add(packages['ansis'], () => ansis.bgGreen(breakStyleAtNewLineFixture)).
262259
run();
263260

264-
bench('RGB colors').add(packages['chalk'], () => {
265-
for (let i = 0; i < 256; i++) chalk.rgb(i, 150, 200)('foo');
266-
}).add(packages['ansis'], () => {
267-
for (let i = 0; i < 256; i++) rgb(i, 150, 200)('foo');
268-
}).run();
269-
270-
// HEX colors
271-
// the hex(), rgb(), bgHex(), bgRgb() methods support only chalk and ansis
272-
bench('HEX color: #FBA').
273-
add(packages['chalk'], () => chalk.hex('#FBA')('foo')).
274-
add(packages['ansis'], () => hex('#FBA')('foo')).
261+
// Template literals, correctly works only with ansis
262+
bench('Template literals').
263+
add(packages['ansis'], () => red`red ${yellow`yellow ${cyan`cyan`} yellow`} red`).
275264
run();
276-
277-
bench('HEX color: #FBAFBA').
278-
add(packages['chalk'], () => chalk.hex('#FBAFBA')('foo')).
279-
add(packages['ansis'], () => hex('#FBAFBA')('foo')).
280-
run();
281-
282-
// // Spectrum HEX colors
283-
// bench('Spectrum HEX colors').
284-
// add(packages['chalk'], () => {
285-
// let str = '';
286-
// spectrum.forEach(color => {
287-
// str += chalk.hex(color)('█');
288-
// });
289-
// return str;
290-
// }).
291-
// add(packages['ansis'], () => {
292-
// let str = '';
293-
// spectrum.forEach(color => {
294-
// str += hex(color)('█');
295-
// });
296-
// return str;
297-
// }).
298-
// run();
299-
300-
// // Template literals
301-
// bench('Template literals').
302-
// add(packages['ansis'], () => red`red ${yellow`yellow ${green`green`} yellow`} red`).
303-
// run();

bench/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"bench": "node ./index.js",
88
"bench:truecolor": "node truecolor.bench.js",
9-
"bench:vi": "vitest bench"
9+
"bench:vi": "vitest bench vitest.bench.js"
1010
},
1111
"dependencies": {
1212
"@colors/colors": "1.6.0",
@@ -21,6 +21,6 @@
2121
"kleur": "4.1.5",
2222
"kolorist": "1.8.0",
2323
"picocolors": "1.1.1",
24-
"vitest": "^3.1.4"
24+
"vitest": "^3.2.0"
2525
}
2626
}

bench/truecolor.bench.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
'use strict';
22

3+
/**
4+
* Note: the hex(), rgb(), bgHex(), bgRgb() methods support only chalk and ansis.
5+
*/
6+
37
import Bench from './lib/bench.js';
48

59
import chalk from 'chalk';
610
import { Ansis, cyan, red, yellow, hex } from 'ansis';
711

12+
import spectrum from '../examples/fixtures/spectrum.js';
813
import { colorLevels, LEVEL_TRUECOLOR } from '../src/color-levels.js';
14+
import packages from './packages.js';
915

1016
const log = console.log;
1117

@@ -36,13 +42,33 @@ log();
3642
log(hex('#F88').inverse.bold` -= Benchmark =- `);
3743

3844
bench('RGB colors').
39-
add('chalk', () => chalk.rgb(99, 150, 200)('foo')).
40-
add('ansis', () => ansis.rgb(99, 150, 200)('foo')).
45+
add(packages['chalk'], () => chalk.rgb(99, 150, 200)('foo')).
46+
add(packages['ansis'], () => ansis.rgb(99, 150, 200)('foo')).
47+
run();
48+
49+
bench('HEX color: #FBA').
50+
add(packages['chalk'], () => chalk.hex('#FBA')('foo')).
51+
add(packages['ansis'], () => ansis.hex('#FBA')('foo')).
52+
run();
53+
54+
bench('HEX color: #FBAFBA').
55+
add(packages['chalk'], () => chalk.hex('#FBAFBA')('foo')).
56+
add(packages['ansis'], () => ansis.hex('#FBAFBA')('foo')).
4157
run();
4258

43-
bench('HEX colors').
44-
add('chalk #FBA', () => chalk.hex('#FBA')('foo')).
45-
add('ansis #FBA', () => ansis.hex('#FBA')('foo')).
46-
add('chalk #FBA123', () => chalk.hex('#FBA123')('foo')).
47-
add('ansis #FBA123', () => ansis.hex('#FBA123')('foo')).
59+
bench('Spectrum HEX colors').
60+
add(packages['chalk'], () => {
61+
let str = '';
62+
spectrum.forEach(color => {
63+
str += chalk.hex(color)('█');
64+
});
65+
return str;
66+
}).
67+
add(packages['ansis'], () => {
68+
let str = '';
69+
spectrum.forEach(color => {
70+
str += ansis.hex(color)('█');
71+
});
72+
return str;
73+
}).
4874
run();

bench/vitest.bench.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import cliColor from 'cli-color';
99
import colorCli from 'colors-cli/safe.js';
1010
import kleur from 'kleur';
1111
import picocolors from 'picocolors';
12-
import * as kolorist from 'kolorist';
1312
import { Ansis } from 'ansis';
1413

1514
// create a new instance of Ansis for correct measure in benchmark
@@ -23,9 +22,9 @@ const options = {
2322
time: CI ? 2000 : 500,
2423
};
2524

26-
// TODO:
27-
// fix - the bench with `kolorist` occurs fatal error: Allocation failed - JavaScript heap out of memory
28-
// fix - unreal/FALSE bench results, e.g. ansis cannot be 3x faster than chalk with the `lib.red('foo')` test
25+
// WARNING
26+
// The bench results are unreal/FALSE!
27+
// E.g., ansis cannot be 3x faster than chalk with the `lib.red('foo')` test.
2928

3029
describe(`Using 1 style (red)`, () => {
3130
bench(packages['colors'], () => colorsJs.red('foo'), options);
@@ -35,7 +34,6 @@ describe(`Using 1 style (red)`, () => {
3534
bench(packages['colors-cli'], () => colorCli.red('foo'), options);
3635
bench(packages['ansi-colors'], () => ansiColors.red('foo'), options);
3736
bench(packages['kleur'], () => kleur.red('foo'), options);
38-
//bench(packages['kolorist'], () =>kolorist.red('foo'), options);
3937
bench(packages['chalk'], () => chalk.red('foo'), options);
4038
bench(packages['ansis'], () => ansis.red('foo'), options);
4139
});
@@ -48,7 +46,6 @@ describe(`Using 2 styles (red, bold)`, () => {
4846
bench(packages['colors-cli'], () => colorCli.red.bold('foo'), options);
4947
bench(packages['ansi-colors'], () => ansiColors.red.bold('foo'), options);
5048
bench(packages['kleur'], () => kleur.red().bold('foo'), options);
51-
//bench(packages['kolorist'], () =>kolorist.red(kolorist.bold('foo')), options);
5249
bench(packages['chalk'], () => chalk.red.bold('foo'), options);
5350
bench(packages['ansis'], () => ansis.red.bold('foo'), options);
5451
});
@@ -61,7 +58,6 @@ describe(`Using 3 styles (red, bold, underline)`, () => {
6158
bench(packages['colors-cli'], () => colorCli.red.bold.underline('foo'), options);
6259
bench(packages['ansi-colors'], () => ansiColors.red.bold.underline('foo'), options);
6360
bench(packages['kleur'], () => kleur.red().bold().underline('foo'), options);
64-
//bench(packages['kolorist'], () =>kolorist.red(kolorist.bold(kolorist.underline('foo'))), options);
6561
bench(packages['chalk'], () => chalk.red.bold.underline('foo'), options);
6662
bench(packages['ansis'], () => ansis.red.bold.underline('foo'), options);
6763
});
@@ -76,7 +72,6 @@ describe(`Using 7 styles (bgWhite red, bold, italic, dim, underline, strikethrou
7672
bench(packages['colors-cli'], () => colorCli.white_b.red.bold.italic.inverse.underline.strike('foo'), options);
7773
bench(packages['ansi-colors'], () => ansiColors.bgWhite.red.bold.italic.dim.underline.strikethrough('foo'), options);
7874
bench(packages['kleur'], () => kleur.bgWhite().red().bold().italic().dim().underline().strikethrough('foo'), options);
79-
//bench(packages['kolorist'], () =>kolorist.bgWhite(kolorist.red(kolorist.bold(kolorist.italic(kolorist.dim(kolorist.underline(kolorist.strikethrough('foo'))))))), options);
8075
bench(packages['chalk'], () => chalk.bgWhite.red.bold.italic.dim.underline.strikethrough('foo'), options);
8176
bench(packages['ansis'], () => ansis.bgWhite.red.bold.italic.dim.underline.strikethrough('foo'), options);
8277
});

bench/vitest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)