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
];
48 * Decide how many rules to show in the output list.
49 * @returns {number} the number of rules to show
51 function getListSize() {
52 const MINIMUM_SIZE
= 10;
54 if (typeof process
.env
.TIMING
!== "string") {
58 if (process
.env
.TIMING
.toLowerCase() === "all") {
59 return Number
.POSITIVE_INFINITY
;
62 const TIMING_ENV_VAR_AS_INTEGER
= Number
.parseInt(process
.env
.TIMING
, 10);
64 return TIMING_ENV_VAR_AS_INTEGER
> 10 ? TIMING_ENV_VAR_AS_INTEGER
: MINIMUM_SIZE
;
67 /* istanbul ignore next */
70 * @param {Object} data Data object to be displayed
71 * @returns {void} prints modified string with console.log
74 function display(data
) {
76 const rows
= Object
.keys(data
)
78 const time
= data
[key
];
83 .sort((a
, b
) => b
[1] - a
[1])
84 .slice(0, getListSize());
87 row
.push(`${(row[1] * 100 / total).toFixed(1)}%`);
88 row
[1] = row
[1].toFixed(3);
91 rows
.unshift(HEADERS
);
96 const len
= row
.length
;
98 for (let i
= 0; i
< len
; i
++) {
99 const n
= row
[i
].length
;
101 if (!widths
[i
] || n
> widths
[i
]) {
107 const table
= rows
.map(row
=> (
109 .map((cell
, index
) => ALIGN
[index
](cell
, widths
[index
]))
113 table
.splice(1, 0, widths
.map((width
, index
) => {
114 const extraAlignment
= index
!== 0 && index
!== widths
.length
- 1 ? 2 : 1;
116 return ALIGN
[index
](":", width
+ extraAlignment
, "-");
119 console
.log(table
.join("\n")); // eslint-disable-line no-console
122 /* istanbul ignore next */
123 module
.exports
= (function() {
125 const data
= Object
.create(null);
129 * @param {*} key key from the data object
130 * @param {Function} fn function to be called
131 * @returns {Function} function to be executed
134 function time(key
, fn
) {
135 if (typeof data
[key
] === "undefined") {
139 return function(...args
) {
140 let t
= process
.hrtime();
143 t
= process
.hrtime(t
);
144 data
[key
] += t
[0] * 1e3
+ t
[1] / 1e6
;
149 process
.on("exit", () => {