@@ -38,6 +38,7 @@ const {
3838 ObjectValues,
3939 ReflectApply,
4040 RegExpPrototypeExec,
41+ SafeMap,
4142 StringPrototypeSlice,
4243 StringPrototypeToWellFormed,
4344} = primordials ;
@@ -118,7 +119,25 @@ const kBoldCode = 1;
118119const kHexCloseSeq = kEscape + '39' + kEscapeEnd ;
119120
120121let styleCache ;
121- const hexStyleCache = { __proto__ : null } ;
122+
123+ const kHexStyleCacheMax = 256 ;
124+
125+ class HexStyleFIFOCache {
126+ #max = kHexStyleCacheMax ;
127+ #cache = new SafeMap ( ) ;
128+
129+ get ( key ) {
130+ return this . #cache. get ( key ) ;
131+ }
132+
133+ set ( key , value ) {
134+ if ( this . #cache. size >= this . #max)
135+ this . #cache. delete ( this . #cache. keys ( ) . next ( ) . value ) ;
136+ this . #cache. set ( key , value ) ;
137+ }
138+ }
139+
140+ const hexStyleCache = new HexStyleFIFOCache ( ) ;
122141
123142function getStyleCache ( ) {
124143 if ( styleCache === undefined ) {
@@ -148,15 +167,15 @@ function getStyleCache() {
148167 * @returns {{openSeq: string, closeSeq: string} }
149168 */
150169function getHexStyle ( hex ) {
151- const cached = hexStyleCache [ hex ] ;
170+ const cached = hexStyleCache . get ( hex ) ;
152171 if ( cached !== undefined ) return cached ;
153172 const { 0 : r , 1 : g , 2 : b } = hexToRgb ( hex ) ;
154173 const style = {
155174 __proto__ : null ,
156175 openSeq : kEscape + rgbToAnsi24Bit ( r , g , b ) + kEscapeEnd ,
157176 closeSeq : kHexCloseSeq ,
158177 } ;
159- hexStyleCache [ hex ] = style ;
178+ hexStyleCache . set ( hex , style ) ;
160179 return style ;
161180}
162181
@@ -241,7 +260,7 @@ function styleText(format, text, options) {
241260 }
242261
243262 if ( format [ 0 ] === '#' ) {
244- let hexStyle = hexStyleCache [ format ] ;
263+ let hexStyle = hexStyleCache . get ( format ) ;
245264 if ( hexStyle === undefined && RegExpPrototypeExec ( hexColorRegExp , format ) !== null ) {
246265 hexStyle = getHexStyle ( format ) ;
247266 }
@@ -281,7 +300,7 @@ function styleText(format, text, options) {
281300 if ( key === 'none' ) continue ;
282301
283302 if ( typeof key === 'string' && key [ 0 ] === '#' ) {
284- let hexStyle = hexStyleCache [ key ] ;
303+ let hexStyle = hexStyleCache . get ( key ) ;
285304 if ( hexStyle === undefined ) {
286305 if ( RegExpPrototypeExec ( hexColorRegExp , key ) === null ) {
287306 throw new ERR_INVALID_ARG_VALUE ( 'format' , key ,
0 commit comments