Skip to content

Commit bda66ad

Browse files
authored
benchmark: add style-text benchmark
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>
1 parent 5d13419 commit bda66ad

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

benchmark/util/style-text.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
5+
const { styleText } = require('node:util');
6+
7+
const bench = common.createBenchmark(main, {
8+
messageType: ['string', 'number', 'boolean', 'invalid'],
9+
format: ['red', 'italic', 'invalid'],
10+
n: [1e3],
11+
});
12+
13+
function main({ messageType, format, n }) {
14+
let str;
15+
switch (messageType) {
16+
case 'string':
17+
str = 'hello world';
18+
break;
19+
case 'number':
20+
str = 10;
21+
break;
22+
case 'boolean':
23+
str = true;
24+
break;
25+
case 'invalid':
26+
str = undefined;
27+
break;
28+
}
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
try {
33+
styleText(format, str);
34+
} catch {
35+
// eslint-disable no-empty
36+
}
37+
}
38+
bench.end(n);
39+
}

0 commit comments

Comments
 (0)