This repository was archived by the owner on Dec 10, 2021. It is now read-only.
forked from atlassian/react-beautiful-dnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimings.js
More file actions
86 lines (73 loc) · 1.83 KB
/
timings.js
File metadata and controls
86 lines (73 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// @flow
type Records = {
[key: string]: number,
};
const records: Records = {};
let isEnabled: boolean = false;
const isTimingsEnabled = (): boolean => isEnabled;
export const forceEnable = () => {
isEnabled = true;
};
// Debug: uncomment to enable
// forceEnable();
export const start = (key: string) => {
// we want to strip all the code out for production builds
// draw back: can only do timings in dev env (which seems to be fine for now)
if (process.env.NODE_ENV !== 'production') {
if (!isTimingsEnabled()) {
return;
}
const now: number = performance.now();
records[key] = now;
}
};
type Style = {|
textColor: string,
symbol: string,
|};
export const finish = (key: string) => {
if (process.env.NODE_ENV !== 'production') {
if (!isTimingsEnabled()) {
return;
}
const now: number = performance.now();
const previous: ?number = records[key];
if (!previous) {
// eslint-disable-next-line no-console
console.warn('cannot finish timing as no previous time found', key);
return;
}
const result: number = now - previous;
const rounded: string = result.toFixed(2);
const style: Style = (() => {
if (result < 12) {
return {
textColor: 'green',
symbol: '✅',
};
}
if (result < 40) {
return {
textColor: 'orange',
symbol: '⚠️',
};
}
return {
textColor: 'red',
symbol: '❌',
};
})();
// eslint-disable-next-line no-console
console.log(
`${style.symbol} %cTiming %c${rounded} %cms %c${key}`,
// title
'color: blue; font-weight: bold;',
// result
`color: ${style.textColor}; font-size: 1.1em;`,
// ms
'color: grey;',
// key
'color: purple; font-weight: bold;',
);
}
};