Skip to content

Commit 28a21d3

Browse files
committed
chore: micro optimisation of performnce for hex()
1 parent 764a64a commit 28a21d3

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

bench/index.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,28 +253,33 @@ bench('Picocolors complex bench').
253253
// Check support of correct break style at new line
254254

255255
// Break style at new line
256-
// const breakStyleAtNewLineFixture = `\nAnsis\nNEW LINE\nNEXT NEW LINE\n`;
257-
// bench('New Line').
258-
// add('colors.js', () => colorsJs.bgGreen(breakStyleAtNewLineFixture)).
259-
// add(packages['ansi-colors'], () => ansiColors.bgGreen(breakStyleAtNewLineFixture)).
260-
// add(packages['chalk'], () => chalk.bgGreen(breakStyleAtNewLineFixture)).
261-
// // 2x slower as chalk because chalk use own implementation, but ansis save 400 bytes and uses regexp, this speed is not critical
262-
// add(packages['ansis'], () => ansis.bgGreen(breakStyleAtNewLineFixture)).
263-
// run();
256+
const breakStyleAtNewLineFixture = `\nAnsis\nNEW LINE\nNEXT NEW LINE\n`;
257+
bench('New Line').
258+
add('colors.js', () => colorsJs.bgGreen(breakStyleAtNewLineFixture)).
259+
add(packages['ansi-colors'], () => ansiColors.bgGreen(breakStyleAtNewLineFixture)).
260+
add(packages['chalk'], () => chalk.bgGreen(breakStyleAtNewLineFixture)).
261+
// 2x slower as chalk because chalk use own implementation, but ansis save 400 bytes and uses regexp, this speed is not critical
262+
add(packages['ansis'], () => ansis.bgGreen(breakStyleAtNewLineFixture)).
263+
run();
264264

265-
// bench('RGB colors').add(packages['chalk'], () => {
266-
// for (let i = 0; i < 256; i++) chalk.rgb(i, 150, 200)('foo');
267-
// }).add(packages['ansis'], () => {
268-
// for (let i = 0; i < 256; i++) rgb(i, 150, 200)('foo');
269-
// }).run();
270-
//
271-
// // HEX colors
265+
bench('RGB colors').add(packages['chalk'], () => {
266+
for (let i = 0; i < 256; i++) chalk.rgb(i, 150, 200)('foo');
267+
}).add(packages['ansis'], () => {
268+
for (let i = 0; i < 256; i++) rgb(i, 150, 200)('foo');
269+
}).run();
270+
271+
// HEX colors
272272
// the hex(), rgb(), bgHex(), bgRgb() methods support only chalk and ansis
273-
bench('HEX colors').
273+
bench('HEX color: #FBA').
274274
add(packages['chalk'], () => chalk.hex('#FBA')('foo')).
275275
add(packages['ansis'], () => hex('#FBA')('foo')).
276276
run();
277277

278+
bench('HEX color: #FBAFBA').
279+
add(packages['chalk'], () => chalk.hex('#FBAFBA')('foo')).
280+
add(packages['ansis'], () => hex('#FBAFBA')('foo')).
281+
run();
282+
278283
// // Spectrum HEX colors
279284
// bench('Spectrum HEX colors').
280285
// add(packages['chalk'], () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ansis",
3-
"version": "4.0.0-rc.3",
3+
"version": "4.0.0-rc.4",
44
"description": "A small and fast library for applying ANSI colors in terminal or browser console",
55
"keywords": [
66
"ansi",

package.npm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"ansis",
3-
"version":"4.0.0-rc.3",
3+
"version":"4.0.0-rc.4",
44
"description":"ANSI color lib",
55
"keywords":["ansi","color","cli"],
66
"license":"ISC",

src/utils.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ let { round, max } = Math;
1515
* @return {[number, number, number]} The red, green, blue values in range [0, 255] .
1616
*/
1717
export let hexToRgb = (value) => {
18-
let color = /([a-f\d]{3,6})/i.exec(value)?.[1] ?? '';
19-
let len = color.length;
18+
let color = /([a-f\d]{3,6})/i.exec(value)?.[1];
19+
let len = color?.length;
2020

21-
let hex = 3 === len
22-
? color[0] + color[0] + color[1] + color[1] + color[2] + color[2]
23-
// faster than 6 !== len
24-
: 6 ^ len ? '0': color;
21+
// Optimisation: `n ^ 6` is faster then `n !== 6`
22+
let hex =
23+
// if len !== 6
24+
6 ^ len
25+
// if len !== 3
26+
? 3 ^ len
27+
? '0' // len !== 6 && len !== 3
28+
: color[0] + color[0] + color[1] + color[1] + color[2] + color[2] // len === 3
29+
: color // len === 6
2530

2631
let decimal = parseInt(hex, 16);
2732

@@ -30,26 +35,28 @@ export let hexToRgb = (value) => {
3035

3136
/**
3237
* Convert RGB values to approximate code of ANSI 256 colors.
33-
* https://github.com/Qix-/color-convert/blob/master/conversions.js#L551
38+
* Optimized version of https://github.com/Qix-/color-convert/blob/master/conversions.js#L551
3439
*
3540
* @param {number} r
3641
* @param {number} g
3742
* @param {number} b
3843
* @return {number}
3944
*/
4045
export let rgbToAnsi256 = (r, g, b) => {
41-
// grayscale
42-
if (r === g && g === b) {
43-
if (8 > r) return 16;
44-
if (r > 248) return 231;
45-
return round(((r - 8) * 24) / 247) + 232;
46+
// r !== g || g !== b
47+
if (r ^ g || g ^ b) {
48+
return 16
49+
// r / 255 * 5 => r / 51
50+
+ (36 * round(r / 51))
51+
+ (6 * round(g / 51))
52+
+ round(b / 51);
4653
}
4754

48-
return 16
49-
// r / 255 * 5 => r / 51
50-
+ (36 * round(r / 51))
51-
+ (6 * round(g / 51))
52-
+ round(b / 51);
55+
// grayscale
56+
57+
if (8 > r) return 16;
58+
if (r > 248) return 231;
59+
return round(((r - 8) * 24) / 247) + 232;
5360
};
5461

5562
/**

0 commit comments

Comments
 (0)