Skip to content

Commit

Permalink
backport #3962
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 22, 2024
1 parent 86a372a commit 25f6058
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion benches/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export function afterFrameAsync() {
return promise;
}

const majorTask = () =>
new Promise(resolve => {
window.addEventListener('message', resolve, { once: true });
window.postMessage('major task delay', '*');
});

let count = 0;
const channel = new MessageChannel();
const callbacks = new Map();
Expand Down Expand Up @@ -64,10 +70,20 @@ export async function mutateAndLayout(mutation, times = 1) {
await forceLayout();
}

export function measureMemory() {
export async function measureMemory() {
if ('gc' in window && 'memory' in performance) {
// Report results in MBs
performance.mark('gc-start');
window.gc();
performance.measure('gc', 'gc-start');

// window.gc synchronously triggers one Major GC. However that MajorGC
// asynchronously triggers additional MajorGCs until the
// usedJSHeapSizeBefore and usedJSHeapSizeAfter are the same. Here, we'll
// wait a moment for some (hopefully all) additional GCs to finish before
// measuring the memory.
await majorTask();
performance.mark('measure-memory');
window.usedJSHeapSize = performance.memory.usedJSHeapSize / 1e6;
} else {
window.usedJSHeapSize = 0;
Expand Down

0 comments on commit 25f6058

Please sign in to comment.