PR-URL: https://github.com/nodejs/node/pull/61556 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Claudio Wunder <cwunder@gnome.org> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
43 lines
937 B
JavaScript
43 lines
937 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
|
|
const { styleText } = require('node:util');
|
|
const assert = require('node:assert');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
messageType: ['string', 'number', 'boolean', 'invalid'],
|
|
format: ['red', 'italic', 'invalid', '#ff0000'],
|
|
validateStream: [1, 0],
|
|
n: [1e3],
|
|
});
|
|
|
|
function main({ messageType, format, validateStream, 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++) {
|
|
let colored = '';
|
|
try {
|
|
colored = styleText(format, str, { validateStream });
|
|
assert.ok(colored); // Attempt to avoid dead-code elimination
|
|
} catch {
|
|
// eslint-disable no-empty
|
|
}
|
|
}
|
|
bench.end(n);
|
|
}
|