Skip to content

Commit ef34ff0

Browse files
util(styleText): optimise + benchmark
Co-Authored-By: Antoine du Hamel <[email protected]>
1 parent 067a779 commit ef34ff0

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

benchmark/util/style-text.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ const { styleText } = require('node:util');
66
const assert = require('node:assert');
77

88
const bench = common.createBenchmark(main, {
9-
messageType: ['string', 'number', 'boolean', 'invalid'],
10-
format: ['red', 'italic', 'invalid'],
11-
validateStream: [1, 0],
12-
n: [1e3],
13-
});
9+
withColor: {
10+
messageType: ['string', 'number', 'boolean', 'invalid'],
11+
format: ['red', 'italic', 'invalid'],
12+
validateStream: [1, 0],
13+
noColor: [''],
14+
n: [1e3],
15+
},
16+
withoutColor: {
17+
messageType: ['string', 'number', 'boolean', 'invalid'],
18+
format: ['red', 'italic', 'invalid'],
19+
validateStream: [1, 0],
20+
noColor: ['1'],
21+
n: [1e3],
22+
},
23+
}, { byGroups: true });
1424

15-
function main({ messageType, format, validateStream, n }) {
25+
function main({ messageType, format, validateStream, noColor, n }) {
1626
let str;
1727
switch (messageType) {
1828
case 'string':
@@ -29,6 +39,8 @@ function main({ messageType, format, validateStream, n }) {
2939
break;
3040
}
3141

42+
process.env.NO_COLOR = noColor;
43+
3244
bench.start();
3345
for (let i = 0; i < n; i++) {
3446
let colored = '';

lib/util.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
119119
validateString(text, 'text');
120120
validateBoolean(validateStream, 'options.validateStream');
121121

122-
let skipColorize;
122+
let shouldColorize = true;
123123
if (validateStream) {
124124
if (
125125
!isReadableStream(stream) &&
@@ -130,26 +130,29 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
130130
}
131131

132132
// If the stream is falsy or should not be colorized, set skipColorize to true
133-
skipColorize = !lazyUtilColors().shouldColorize(stream);
133+
shouldColorize = lazyUtilColors().shouldColorize(stream);
134134
}
135135

136136
// If the format is not an array, convert it to an array
137137
const formatArray = ArrayIsArray(format) ? format : [format];
138+
const { colors } = inspect;
138139

139-
let left = '';
140-
let right = '';
141-
for (const key of formatArray) {
142-
const formatCodes = inspect.colors[key];
140+
// We want to loop through the format array to check if the format is valid
141+
// including if it's shouldn't be colorized
142+
const { left, right } = formatArray.reduce((acc, key) => {
143+
const formatCodes = colors[key];
143144
// If the format is not a valid style, throw an error
144145
if (formatCodes == null) {
145-
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
146+
validateOneOf(key, 'format', ObjectKeys(colors));
146147
}
147-
if (skipColorize) continue;
148-
left += escapeStyleCode(formatCodes[0]);
149-
right = `${escapeStyleCode(formatCodes[1])}${right}`;
150-
}
148+
if(shouldColorize) {
149+
acc.left += escapeStyleCode(formatCodes[0]);
150+
acc.right = `${escapeStyleCode(formatCodes[1])}${acc.right}`;
151+
}
152+
return acc;
153+
}, { left: '', right: '' });
151154

152-
return skipColorize ? text : `${left}${text}${right}`;
155+
return shouldColorize ? `${left}${text}${right}` : text;
153156
}
154157

155158
/**

0 commit comments

Comments
 (0)