-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathaddMemoryEntry.ts
50 lines (46 loc) · 1.56 KB
/
addMemoryEntry.ts
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
import { WINDOW } from '../constants';
import type { AddEventResult, MemoryData, ReplayContainer, ReplayPerformanceEntry } from '../types';
import { createPerformanceSpans } from './createPerformanceSpans';
type ReplayMemoryEntry = ReplayPerformanceEntry<MemoryData> & { data: { memory: MemoryInfo } };
interface MemoryInfo {
jsHeapSizeLimit: number;
totalJSHeapSize: number;
usedJSHeapSize: number;
}
/**
* Create a "span" for the total amount of memory being used by JS objects
* (including v8 internal objects).
*/
export async function addMemoryEntry(replay: ReplayContainer): Promise<Array<AddEventResult | null>> {
// window.performance.memory is a non-standard API and doesn't work on all browsers, so we try-catch this
try {
return Promise.all(
createPerformanceSpans(replay, [
// @ts-expect-error memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
createMemoryEntry(WINDOW.performance.memory),
]),
);
} catch (error) {
// Do nothing
return [];
}
}
function createMemoryEntry(memoryEntry: MemoryInfo): ReplayMemoryEntry {
const { jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize } = memoryEntry;
// we don't want to use `getAbsoluteTime` because it adds the event time to the
// time origin, so we get the current timestamp instead
const time = Date.now() / 1000;
return {
type: 'memory',
name: 'memory',
start: time,
end: time,
data: {
memory: {
jsHeapSizeLimit,
totalJSHeapSize,
usedJSHeapSize,
},
},
};
}