-
Notifications
You must be signed in to change notification settings - Fork 90
/
measure-memory-leaks.js
46 lines (37 loc) · 1.26 KB
/
measure-memory-leaks.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
41
42
43
44
45
46
/* eslint-disable */
const puppeteer = require('puppeteer');
// Helper by @chrisguttandin
const countObjects = async (page) => {
const prototypeHandle = await page.evaluateHandle(() => Object.prototype);
const objectsHandle = await page.queryObjects(prototypeHandle);
const numberOfObjects = await page.evaluate((instances) => instances.length, objectsHandle);
await Promise.all([
prototypeHandle.dispose(),
objectsHandle.dispose(),
]);
return numberOfObjects;
};
(async () => {
const browser = await puppeteer.launch();
const page = await browser.createIncognitoBrowserContext();
const numberOfObjects = await countObjects(page);
console.log(numberOfObjects);
await page.evaluate(() => {
class SomeObject {
constructor() {
this.numbers = {};
for (let i = 0; i < 1000; i++) {
this.numbers[Math.random()] = Math.random();
}
}
}
const someObject = new SomeObject();
const onMessage = () => {/* ... */};
window.addEventListener('message', onMessage);
});
const numberOfObjectsAfter = await countObjects(page);
console.log(numberOfObjectsAfter);
// Check if the number of retained objects is expected
// expect(await countObjects(page)).to.equal(0);
await browser.close();
})();