Skip to content

Commit 38758a7

Browse files
araujoguiaduh95
authored andcommitted
util: remove style caches from styleText slow path
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #63706 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent d5be94e commit 38758a7

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

‎lib/util.js‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,24 @@ function getHexStyleCache() {
129129
returnhexStyleCache;
130130
}
131131

132+
functioncodesToStyle(codes){
133+
constopenNum=codes[0];
134+
return{
135+
__proto__: null,
136+
openSeq: kEscape+openNum+kEscapeEnd,
137+
closeSeq: kEscape+codes[1]+kEscapeEnd,
138+
keepClose: openNum===kDimCode||openNum===kBoldCode,
139+
};
140+
}
141+
132142
functiongetStyleCache(){
133143
if(styleCache===undefined){
134144
styleCache={__proto__: null};
135145
constcolors=inspect.colors;
136146
for(constkeyofObjectGetOwnPropertyNames(colors)){
137147
constcodes=colors[key];
138148
if(codes){
139-
constopenNum=codes[0];
140-
constcloseNum=codes[1];
141-
styleCache[key]={
142-
__proto__: null,
143-
openSeq: kEscape+openNum+kEscapeEnd,
144-
closeSeq: kEscape+closeNum+kEscapeEnd,
145-
keepClose: openNum===kDimCode||openNum===kBoldCode,
146-
};
149+
styleCache[key]=codesToStyle(codes);
147150
}
148151
}
149152
}
@@ -241,10 +244,10 @@ function rgbToAnsi24Bit(r, g, b) {
241244
*/
242245
functionstyleText(format,text,options){
243246
constvalidateStream=options?.validateStream??true;
244-
constcache=getStyleCache();
245247

246248
// Fast path: single format string with validateStream=false
247249
if(!validateStream&&typeofformat==='string'&&typeoftext==='string'){
250+
constcache=getStyleCache();
248251
if(format==='none')returntext;
249252
conststyle=cache[format];
250253
if(style!==undefined){
@@ -284,6 +287,7 @@ function styleText(format, text, options) {
284287
}
285288

286289
constformatArray=ArrayIsArray(format) ? format : [format];
290+
constcolors=inspect.colors;
287291

288292
letopenCodes='';
289293
letcloseCodes='';
@@ -293,30 +297,27 @@ function styleText(format, text, options) {
293297
if(key==='none')continue;
294298

295299
if(typeofkey==='string'&&key[0]==='#'){
296-
lethexStyle=getHexStyleCache().get(key);
297-
if(hexStyle===undefined){
298-
if(RegExpPrototypeExec(hexColorRegExp,key)===null){
299-
thrownewERR_INVALID_ARG_VALUE('format',key,
300-
'must be a valid hex color (#RGB or #RRGGBB)');
301-
}
302-
if(skipColorize)continue;
303-
hexStyle=getHexStyle(key);
304-
}elseif(skipColorize){
305-
continue;
300+
if(RegExpPrototypeExec(hexColorRegExp,key)===null){
301+
thrownewERR_INVALID_ARG_VALUE('format',key,
302+
'must be a valid hex color (#RGB or #RRGGBB)');
306303
}
307-
openCodes+=hexStyle.openSeq;
308-
closeCodes=hexStyle.closeSeq+closeCodes;
309-
processedText=replaceCloseCode(processedText,hexStyle.closeSeq,hexStyle.openSeq,false);
304+
if(skipColorize)continue;
305+
const{0: r,1: g,2: b}=hexToRgb(key);
306+
consthexOpenSeq=kEscape+rgbToAnsi24Bit(r,g,b)+kEscapeEnd;
307+
openCodes+=hexOpenSeq;
308+
closeCodes=kHexCloseSeq+closeCodes;
309+
processedText=replaceCloseCode(processedText,kHexCloseSeq,hexOpenSeq,false);
310310
continue;
311311
}
312312

313-
conststyle=cache[key];
314-
if(style===undefined){
313+
constcodes=colors[key];
314+
if(!codes){
315315
validateOneOf(key,'format',ObjectGetOwnPropertyNames(inspect.colors));
316316
}
317-
openCodes+=style.openSeq;
318-
closeCodes=style.closeSeq+closeCodes;
319-
processedText=replaceCloseCode(processedText,style.closeSeq,style.openSeq,style.keepClose);
317+
const{ openSeq, closeSeq, keepClose }=codesToStyle(codes);
318+
openCodes+=openSeq;
319+
closeCodes=closeSeq+closeCodes;
320+
processedText=replaceCloseCode(processedText,closeSeq,openSeq,keepClose);
320321
}
321322

322323
if(skipColorize)returntext;

0 commit comments

Comments
 (0)