@@ -5,9 +5,22 @@ const common = require('../common.js');
55const { styleText } = require ( 'node:util' ) ;
66const assert = require ( 'node:assert' ) ;
77
8+ // 1000 distinct hex colors to exercise the cache under high-miss conditions.
9+ // Spread evenly across hue space so colors are valid and maximally varied.
10+ const kHexColorCount = 1000 ;
11+ const toHex = ( n ) => n . toString ( 16 ) . padStart ( 2 , '0' ) ;
12+ const hexColors = Array . from ( { length : kHexColorCount } , ( _ , i ) => {
13+ const r = ( i * 37 ) & 0xff ;
14+ const g = ( i * 73 ) & 0xff ;
15+ const b = ( i * 137 ) & 0xff ;
16+ return `#${ toHex ( r ) } ${ toHex ( g ) } ${ toHex ( b ) } ` ;
17+ } ) ;
18+
819const bench = common . createBenchmark ( main , {
920 messageType : [ 'string' , 'number' , 'boolean' , 'invalid' ] ,
10- format : [ 'red' , 'italic' , 'invalid' , '#ff0000' ] ,
21+ // '#rotating' cycles through kHexColorCount distinct colors to simulate
22+ // the high-miss-rate / large-cache scenario (e.g. user-randomised colors).
23+ format : [ 'red' , 'italic' , 'invalid' , '#ff0000' , '#rotating' ] ,
1124 validateStream : [ 1 , 0 ] ,
1225 n : [ 1e3 ] ,
1326} ) ;
@@ -31,9 +44,10 @@ function main({ messageType, format, validateStream, n }) {
3144
3245 bench . start ( ) ;
3346 for ( let i = 0 ; i < n ; i ++ ) {
47+ const fmt = format === '#rotating' ? hexColors [ i % kHexColorCount ] : format ;
3448 let colored = '' ;
3549 try {
36- colored = styleText ( format , str , { validateStream } ) ;
50+ colored = styleText ( fmt , str , { validateStream } ) ;
3751 assert . ok ( colored ) ; // Attempt to avoid dead-code elimination
3852 } catch {
3953 // eslint-disable no-empty
0 commit comments