From ef34ff08e76bc867981c0872b89745ab51fd95b4 Mon Sep 17 00:00:00 2001 From: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Date: Mon, 28 Apr 2025 17:07:27 +0200 Subject: [PATCH 1/3] util(styleText): optimise + benchmark Co-Authored-By: Antoine du Hamel --- benchmark/util/style-text.js | 24 ++++++++++++++++++------ lib/util.js | 27 +++++++++++++++------------ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/benchmark/util/style-text.js b/benchmark/util/style-text.js index 282a96150f0b94..f022890de5209c 100644 --- a/benchmark/util/style-text.js +++ b/benchmark/util/style-text.js @@ -6,13 +6,23 @@ const { styleText } = require('node:util'); const assert = require('node:assert'); const bench = common.createBenchmark(main, { - messageType: ['string', 'number', 'boolean', 'invalid'], - format: ['red', 'italic', 'invalid'], - validateStream: [1, 0], - n: [1e3], -}); + withColor: { + messageType: ['string', 'number', 'boolean', 'invalid'], + format: ['red', 'italic', 'invalid'], + validateStream: [1, 0], + noColor: [''], + n: [1e3], + }, + withoutColor: { + messageType: ['string', 'number', 'boolean', 'invalid'], + format: ['red', 'italic', 'invalid'], + validateStream: [1, 0], + noColor: ['1'], + n: [1e3], + }, +}, { byGroups: true }); -function main({ messageType, format, validateStream, n }) { +function main({ messageType, format, validateStream, noColor, n }) { let str; switch (messageType) { case 'string': @@ -29,6 +39,8 @@ function main({ messageType, format, validateStream, n }) { break; } + process.env.NO_COLOR = noColor; + bench.start(); for (let i = 0; i < n; i++) { let colored = ''; diff --git a/lib/util.js b/lib/util.js index 233da10e83c48d..7fffc6ee168f3f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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) && @@ -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; } /** From b758ffff9bd62c9d27dbf8fcd55ba5ef28acbc2a Mon Sep 17 00:00:00 2001 From: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Date: Wed, 30 Apr 2025 12:25:28 +0200 Subject: [PATCH 2/3] benchmark: update --- benchmark/util/style-text.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/benchmark/util/style-text.js b/benchmark/util/style-text.js index f022890de5209c..447a6d32d8eabc 100644 --- a/benchmark/util/style-text.js +++ b/benchmark/util/style-text.js @@ -5,24 +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, { withColor: { messageType: ['string', 'number', 'boolean', 'invalid'], - format: ['red', 'italic', 'invalid'], + isValidFormat: [1, 0], validateStream: [1, 0], noColor: [''], n: [1e3], }, withoutColor: { messageType: ['string', 'number', 'boolean', 'invalid'], - format: ['red', 'italic', 'invalid'], + isValidFormat: [1, 0], validateStream: [1, 0], noColor: ['1'], n: [1e3], }, }, { byGroups: true }); -function main({ messageType, format, validateStream, noColor, n }) { +function main({ messageType, isValidFormat, validateStream, noColor, n }) { let str; switch (messageType) { case 'string': @@ -40,6 +43,7 @@ function main({ messageType, format, validateStream, noColor, n }) { } process.env.NO_COLOR = noColor; + const format = isValidFormat ? validFormats : invalidFormats; bench.start(); for (let i = 0; i < n; i++) { From a9d13f275a2118c1a9a55c861311bef7f14259a5 Mon Sep 17 00:00:00 2001 From: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Date: Wed, 30 Apr 2025 12:26:26 +0200 Subject: [PATCH 3/3] fix: lint-js --- benchmark/util/style-text.js | 2 +- lib/util.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/util/style-text.js b/benchmark/util/style-text.js index 447a6d32d8eabc..2ca099da97ad33 100644 --- a/benchmark/util/style-text.js +++ b/benchmark/util/style-text.js @@ -5,7 +5,7 @@ const common = require('../common.js'); const { styleText } = require('node:util'); const assert = require('node:assert'); -const validFormats = ['red', 'italic','bold'] +const validFormats = ['red', 'italic', 'bold']; const invalidFormats = 'invalidFormat'; const bench = common.createBenchmark(main, { diff --git a/lib/util.js b/lib/util.js index 7fffc6ee168f3f..ccfe124f175b68 100644 --- a/lib/util.js +++ b/lib/util.js @@ -145,7 +145,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou if (formatCodes == null) { validateOneOf(key, 'format', ObjectKeys(colors)); } - if(shouldColorize) { + if (shouldColorize) { acc.left += escapeStyleCode(formatCodes[0]); acc.right = `${escapeStyleCode(formatCodes[1])}${acc.right}`; }