2 * @fileoverview Tracks performance of individual rules.
3 * @author Brandon Mills
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 /* istanbul ignore next */
14 * Align the string to left
15 * @param {string} str string to evaluate
16 * @param {int} len length of the string
17 * @param {string} ch delimiter character
18 * @returns {string} modified string
21 function alignLeft(str, len, ch) {
22 return str + new Array(len - str.length + 1).join(ch || " ");
25 /* istanbul ignore next */
27 * Align the string to right
28 * @param {string} str string to evaluate
29 * @param {int} len length of the string
30 * @param {string} ch delimiter character
31 * @returns {string} modified string
34 function alignRight(str, len, ch) {
35 return new Array(len - str.length + 1).join(ch || " ") + str;
38 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
42 const enabled = !!process.env.TIMING;
44 const HEADERS = ["Rule", "Time (ms)", "Relative"];
45 const ALIGN = [alignLeft, alignRight, alignRight];
47 /* istanbul ignore next */
50 * @param {Object} data Data object to be displayed
51 * @returns {void} prints modified string with console.log
54 function display(data) {
56 const rows = Object.keys(data)
58 const time = data[key];
63 .sort((a, b) => b[1] - a[1])
67 row.push(`${(row[1] * 100 / total).toFixed(1)}%`);
68 row[1] = row[1].toFixed(3);
71 rows.unshift(HEADERS);
76 const len = row.length;
78 for (let i = 0; i < len; i++) {
79 const n = row[i].length;
81 if (!widths[i] || n > widths[i]) {
87 const table = rows.map(row => (
89 .map((cell, index) => ALIGN[index](cell, widths[index]))
93 table.splice(1, 0, widths.map((width, index) => {
94 const extraAlignment = index !== 0 && index !== widths.length - 1 ? 2 : 1;
96 return ALIGN[index](":", width + extraAlignment, "-");
99 console.log(table.join("\n")); // eslint-disable-line no-console
102 /* istanbul ignore next */
103 module.exports = (function() {
105 const data = Object.create(null);
109 * @param {*} key key from the data object
110 * @param {Function} fn function to be called
111 * @returns {Function} function to be executed
114 function time(key, fn) {
115 if (typeof data[key] === "undefined") {
119 return function(...args) {
120 let t = process.hrtime();
123 t = process.hrtime(t);
124 data[key] += t[0] * 1e3 + t[1] / 1e6;
129 process.on("exit", () => {