-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathcommon.spec.ts
34 lines (30 loc) · 1.05 KB
/
common.spec.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
interface Window {
performance: { memory: any };
}
declare var window: Window
export const inMB = (n: any) => n / 1000000;
function runningAverage(arr: any, newVal: any, oldAvg: any) {
return ((oldAvg * (arr.length - 1) + newVal) / arr.length);
};
export const profile = {
samples: [] as any,
diffs: [] as any,
averageUsage: 0,
averageChange: 0,
//Collects a sample of memory and updates all the values in the
//profile object
sample() {
let newSample = getMemoryProfile();
this.samples.push(newSample);
this.averageUsage = runningAverage(this.samples, newSample, this.averageUsage);
let sampleLen: any = this.samples.length;
if (sampleLen >= 2) {
let newDiff = this.samples[sampleLen - 1] - this.samples[sampleLen - 2];
this.diffs.push(newDiff);
this.averageChange = runningAverage(this.diffs, newDiff, this.averageChange);
}
}
}
export const getMemoryProfile = () => {
return window.performance.memory.usedJSHeapSize; //Return used memory
};