performance.js
1.6 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
/**
* 性能工具
* 1. 计算方法执行时间
* @returns {void}
* @date 2019-11-29
*/
import Timer from './timer'
class Performance {
/**
* 计算情况
* @returns {Function} 执行返回值获取时间信息
* @date 2019-11-29
*/
startExecute (name = '') {
const timer = Timer.start()
const usedJSHeapSize = this.getMemoryInfo().usedJSHeapSize
return (name2 = '') => {
const executeTime = timer.stop()
const endMemoryInfo = this.getMemoryInfo()
console.log('%cPerformance%c \n1. 执行方法:%c%s%c\n2. 执行耗时: %c%sms%c \n3. 内存波动:%sB \n4. 已分配内存: %sMB \n5. 已使用内存:%sMB \n6. 剩余内存: %sMB',
'padding: 2px 4px 2px 4px; background-color: #4caf50; color: #fff; border-radius: 4px;', '',
'color: #ff6f00', `${name} ${name2}`, '',
'color: #ff6f00', executeTime, '',
endMemoryInfo.usedJSHeapSize - usedJSHeapSize,
this.toMBSize(endMemoryInfo.jsHeapSizeLimit),
this.toMBSize(endMemoryInfo.usedJSHeapSize),
this.toMBSize(endMemoryInfo.totalJSHeapSize)
)
}
}
/**
* 获取内存信息
* @returns {void}
* @date 2019-11-29
*/
getMemoryInfo () {
let memoryInfo = {}
if (window.performance && window.performance.memory) {
memoryInfo = window.performance.memory
}
return memoryInfo
}
/**
* 转化为MB
* @returns {void}
* @date 2019-11-29
*/
toMBSize (byteSize) {
return (byteSize / (1024 * 1024)).toFixed(1)
}
}
export default new Performance()