Skip to content

util(styleText): optimise + benchmark #58063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions benchmark/util/style-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ const common = require('../common.js');
const { styleText } = require('node:util');
const assert = require('node:assert');

const validFormats = ['red', 'italic', 'bold'];
const invalidFormats = 'invalidFormat';

const bench = common.createBenchmark(main, {
messageType: ['string', 'number', 'boolean', 'invalid'],
format: ['red', 'italic', 'invalid'],
validateStream: [1, 0],
n: [1e3],
});
withColor: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, create a different file instead. style-text-nocolor.js

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh okay got it !

messageType: ['string', 'number', 'boolean', 'invalid'],
isValidFormat: [1, 0],
validateStream: [1, 0],
noColor: [''],
n: [1e3],
},
withoutColor: {
messageType: ['string', 'number', 'boolean', 'invalid'],
isValidFormat: [1, 0],
validateStream: [1, 0],
noColor: ['1'],
n: [1e3],
},
}, { byGroups: true });

function main({ messageType, format, validateStream, n }) {
function main({ messageType, isValidFormat, validateStream, noColor, n }) {
let str;
switch (messageType) {
case 'string':
Expand All @@ -29,6 +42,9 @@ function main({ messageType, format, validateStream, n }) {
break;
}

process.env.NO_COLOR = noColor;
const format = isValidFormat ? validFormats : invalidFormats;

bench.start();
for (let i = 0; i < n; i++) {
let colored = '';
Expand Down
27 changes: 15 additions & 12 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
validateString(text, 'text');
validateBoolean(validateStream, 'options.validateStream');

let skipColorize;
let shouldColorize = true;
if (validateStream) {
if (
!isReadableStream(stream) &&
Expand All @@ -130,26 +130,29 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
}

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

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

let left = '';
let right = '';
for (const key of formatArray) {
const formatCodes = inspect.colors[key];
// We want to loop through the format array to check if the format is valid
// including if it's shouldn't be colorized
const { left, right } = formatArray.reduce((acc, key) => {
const formatCodes = colors[key];
// If the format is not a valid style, throw an error
if (formatCodes == null) {
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
validateOneOf(key, 'format', ObjectKeys(colors));
}
if (skipColorize) continue;
left += escapeStyleCode(formatCodes[0]);
right = `${escapeStyleCode(formatCodes[1])}${right}`;
}
if (shouldColorize) {
acc.left += escapeStyleCode(formatCodes[0]);
acc.right = `${escapeStyleCode(formatCodes[1])}${acc.right}`;
}
return acc;
}, { left: '', right: '' });

return skipColorize ? text : `${left}${text}${right}`;
return shouldColorize ? `${left}${text}${right}` : text;
}

/**
Expand Down
Loading