Skip to content

Commit 2ea6e52

Browse files
committed
util: improve styleText performance
The validation for when to colorize or not was happening in a loop. Instead, it is now done once up front instead.
1 parent 0c1fb98 commit 2ea6e52

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

lib/util.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
123123
validateString(text, 'text');
124124
validateBoolean(validateStream, 'options.validateStream');
125125

126-
let skipColorize;
127126
if (validateStream) {
128127
if (
129128
!isReadableStream(stream) &&
@@ -134,9 +133,13 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
134133
}
135134

136135
// If the stream is falsy or should not be colorized, set skipColorize to true
137-
skipColorize = !lazyUtilColors().shouldColorize(stream);
136+
const skipColorize = !lazyUtilColors().shouldColorize(stream);
137+
if (skipColorize) {
138+
return text;
139+
}
138140
}
139141

142+
140143
// If the format is not an array, convert it to an array
141144
const formatArray = ArrayIsArray(format) ? format : [format];
142145

@@ -148,14 +151,9 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
148151
if (formatCodes == null) {
149152
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
150153
}
151-
if (skipColorize) continue;
152154
ArrayPrototypePush(codes, formatCodes);
153155
}
154156

155-
if (skipColorize) {
156-
return text;
157-
}
158-
159157
// Build opening codes
160158
let openCodes = '';
161159
for (let i = 0; i < codes.length; i++) {

0 commit comments

Comments
 (0)