-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.js
40 lines (34 loc) · 1.14 KB
/
memory.js
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
let unsupported;
// 检查浏览器是否支持 Navigator.deviceMemory
if (typeof navigator !== "undefined" && "deviceMemory" in navigator) {
unsupported = false;
} else {
unsupported = true;
}
let initialMemoryStatus;
if (!unsupported) {
// 检查浏览器是否支持 performance.memory
const performanceMemory =
"memory" in performance ? performance.memory : null;
initialMemoryStatus = {
deviceMemory: navigator.deviceMemory,
// The maximum size of the heap, in bytes, that is available to the context.
jsHeapSizeLimit: performanceMemory
? performanceMemory.jsHeapSizeLimit
: null,
// The total allocated heap size, in bytes.
totalJSHeapSize: performanceMemory
? performanceMemory.totalJSHeapSize
: null,
// The currently active segment of JS heap, in bytes.
usedJSHeapSize: performanceMemory
? performanceMemory.usedJSHeapSize
: null
};
} else {
initialMemoryStatus = { unsupported };
}
const useMemoryStatus = () => {
return { ...initialMemoryStatus };
};
export { useMemoryStatus };