Skip to content

Commit 521eba3

Browse files
committed
benchmark: create rotating hex benchmark
1 parent 46bc1fd commit 521eba3

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

benchmark/util/style-text.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@ const common = require('../common.js');
55
const { styleText } = require('node:util');
66
const 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+
819
const 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

Comments
 (0)