1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function alignLeft(str, len, ch) {
|
22 | return str + new Array(len - str.length + 1).join(ch || " ");
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function alignRight(str, len, ch) {
|
35 | return new Array(len - str.length + 1).join(ch || " ") + str;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const enabled = !!process.env.TIMING;
|
43 |
|
44 | const HEADERS = ["Rule", "Time (ms)", "Relative"];
|
45 | const ALIGN = [alignLeft, alignRight, alignRight];
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function getListSize() {
|
52 | const MINIMUM_SIZE = 10;
|
53 |
|
54 | if (typeof process.env.TIMING !== "string") {
|
55 | return MINIMUM_SIZE;
|
56 | }
|
57 |
|
58 | if (process.env.TIMING.toLowerCase() === "all") {
|
59 | return Number.POSITIVE_INFINITY;
|
60 | }
|
61 |
|
62 | const TIMING_ENV_VAR_AS_INTEGER = Number.parseInt(process.env.TIMING, 10);
|
63 |
|
64 | return TIMING_ENV_VAR_AS_INTEGER > 10 ? TIMING_ENV_VAR_AS_INTEGER : MINIMUM_SIZE;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function display(data) {
|
75 | let total = 0;
|
76 | const rows = Object.keys(data)
|
77 | .map(key => {
|
78 | const time = data[key];
|
79 |
|
80 | total += time;
|
81 | return [key, time];
|
82 | })
|
83 | .sort((a, b) => b[1] - a[1])
|
84 | .slice(0, getListSize());
|
85 |
|
86 | rows.forEach(row => {
|
87 | row.push(`${(row[1] * 100 / total).toFixed(1)}%`);
|
88 | row[1] = row[1].toFixed(3);
|
89 | });
|
90 |
|
91 | rows.unshift(HEADERS);
|
92 |
|
93 | const widths = [];
|
94 |
|
95 | rows.forEach(row => {
|
96 | const len = row.length;
|
97 |
|
98 | for (let i = 0; i < len; i++) {
|
99 | const n = row[i].length;
|
100 |
|
101 | if (!widths[i] || n > widths[i]) {
|
102 | widths[i] = n;
|
103 | }
|
104 | }
|
105 | });
|
106 |
|
107 | const table = rows.map(row => (
|
108 | row
|
109 | .map((cell, index) => ALIGN[index](cell, widths[index]))
|
110 | .join(" | ")
|
111 | ));
|
112 |
|
113 | table.splice(1, 0, widths.map((width, index) => {
|
114 | const extraAlignment = index !== 0 && index !== widths.length - 1 ? 2 : 1;
|
115 |
|
116 | return ALIGN[index](":", width + extraAlignment, "-");
|
117 | }).join("|"));
|
118 |
|
119 | console.log(table.join("\n"));
|
120 | }
|
121 |
|
122 |
|
123 | module.exports = (function() {
|
124 |
|
125 | const data = Object.create(null);
|
126 |
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function time(key, fn) {
|
135 | if (typeof data[key] === "undefined") {
|
136 | data[key] = 0;
|
137 | }
|
138 |
|
139 | return function(...args) {
|
140 | let t = process.hrtime();
|
141 |
|
142 | fn(...args);
|
143 | t = process.hrtime(t);
|
144 | data[key] += t[0] * 1e3 + t[1] / 1e6;
|
145 | };
|
146 | }
|
147 |
|
148 | if (enabled) {
|
149 | process.on("exit", () => {
|
150 | display(data);
|
151 | });
|
152 | }
|
153 |
|
154 | return {
|
155 | time,
|
156 | enabled,
|
157 | getListSize
|
158 | };
|
159 |
|
160 | }());
|