Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
benchmark: create rotating hex benchmark
  • Loading branch information
araujogui committed May 9, 2026
commit 5b9ea9dbbae728a5182b58e1da011d7f553727e0
18 changes: 16 additions & 2 deletions benchmark/util/style-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ const common = require('../common.js');
const { styleText } = require('node:util');
const assert = require('node:assert');

// 1000 distinct hex colors to exercise the cache under high-miss conditions.
// Spread evenly across hue space so colors are valid and maximally varied.
const kHexColorCount = 1000;
const toHex = (n) => n.toString(16).padStart(2, '0');
const hexColors = Array.from({ length: kHexColorCount }, (_, i) => {
const r = (i * 37) & 0xff;
const g = (i * 73) & 0xff;
const b = (i * 137) & 0xff;
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
});

const bench = common.createBenchmark(main, {
messageType: ['string', 'number', 'boolean', 'invalid'],
format: ['red', 'italic', 'invalid', '#ff0000'],
// '#rotating' cycles through kHexColorCount distinct colors to simulate
// the high-miss-rate / large-cache scenario (e.g. user-randomised colors).
format: ['red', 'italic', 'invalid', '#ff0000', '#rotating'],
validateStream: [1, 0],
n: [1e3],
});
Expand All @@ -31,9 +44,10 @@ function main({ messageType, format, validateStream, n }) {

bench.start();
for (let i = 0; i < n; i++) {
const fmt = format === '#rotating' ? hexColors[i % kHexColorCount] : format;
let colored = '';
try {
colored = styleText(format, str, { validateStream });
colored = styleText(fmt, str, { validateStream });
assert.ok(colored); // Attempt to avoid dead-code elimination
} catch {
// eslint-disable no-empty
Expand Down
Loading