Skip to content

Commit 369b13f

Browse files
perf(util): optimise styleText
1 parent 067a779 commit 369b13f

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

benchmark/util/style-text.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ const bench = common.createBenchmark(main, {
99
messageType: ['string', 'number', 'boolean', 'invalid'],
1010
format: ['red', 'italic', 'invalid'],
1111
validateStream: [1, 0],
12+
noColors: [1, 0],
1213
n: [1e3],
1314
});
1415

15-
function main({ messageType, format, validateStream, n }) {
16+
function main({ messageType, format, validateStream, noColors, n }) {
1617
let str;
1718
switch (messageType) {
1819
case 'string':
@@ -29,6 +30,8 @@ function main({ messageType, format, validateStream, n }) {
2930
break;
3031
}
3132

33+
process.env.NO_COLORS = noColors ? true : false;
34+
3235
bench.start();
3336
for (let i = 0; i < n; i++) {
3437
let colored = '';

lib/util.js

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

122-
let skipColorize;
123122
if (validateStream) {
124123
if (
125124
!isReadableStream(stream) &&
@@ -129,8 +128,9 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
129128
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
130129
}
131130

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

136136
// If the format is not an array, convert it to an array
@@ -144,12 +144,12 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
144144
if (formatCodes == null) {
145145
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
146146
}
147-
if (skipColorize) continue;
147+
148148
left += escapeStyleCode(formatCodes[0]);
149149
right = `${escapeStyleCode(formatCodes[1])}${right}`;
150150
}
151151

152-
return skipColorize ? text : `${left}${text}${right}`;
152+
return `${left}${text}${right}`;
153153
}
154154

155155
/**

test/parallel/test-util-styletext.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ assert.throws(() => {
3535
}, {
3636
code: 'ERR_INVALID_ARG_VALUE',
3737
});
38+
assert.throws(() => {
39+
util.styleText(['invalid'], 'text');
40+
}, {
41+
code: 'ERR_INVALID_ARG_VALUE',
42+
});
3843

3944
assert.strictEqual(
4045
util.styleText('red', 'test', { validateStream: false }),
4146
'\u001b[31mtest\u001b[39m',
4247
);
43-
4448
assert.strictEqual(
4549
util.styleText(['bold', 'red'], 'test', { validateStream: false }),
4650
'\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m',
@@ -55,21 +59,14 @@ assert.strictEqual(
5559
),
5660
);
5761

58-
assert.throws(() => {
59-
util.styleText(['invalid'], 'text');
60-
}, {
61-
code: 'ERR_INVALID_ARG_VALUE',
62-
});
63-
6462
assert.throws(() => {
6563
util.styleText('red', 'text', { stream: {} });
6664
}, {
6765
code: 'ERR_INVALID_ARG_TYPE',
6866
});
6967

70-
// does not throw
68+
// doesn't throw
7169
util.styleText('red', 'text', { stream: {}, validateStream: false });
72-
7370
assert.strictEqual(
7471
util.styleText('red', 'test', { validateStream: false }),
7572
styled,

0 commit comments

Comments
 (0)