Skip to content

Commit

Permalink
benchmark: add style-text benchmark
Browse files Browse the repository at this point in the history
PR-URL: #52004
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
RafaelGSS authored and marco-ippolito committed May 2, 2024
1 parent a223ca4 commit c00715c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions benchmark/util/style-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const common = require('../common.js');

const { styleText } = require('node:util');

const bench = common.createBenchmark(main, {
messageType: ['string', 'number', 'boolean', 'invalid'],
format: ['red', 'italic', 'invalid'],
n: [1e3],
});

function main({ messageType, format, n }) {
let str;
switch (messageType) {
case 'string':
str = 'hello world';
break;
case 'number':
str = 10;
break;
case 'boolean':
str = true;
break;
case 'invalid':
str = undefined;
break;
}

bench.start();
for (let i = 0; i < n; i++) {
try {
styleText(format, str);
} catch {
// eslint-disable no-empty
}
}
bench.end(n);
}

0 comments on commit c00715c

Please sign in to comment.