Last active
January 25, 2024 23:55
-
-
Save acdcjunior/3953064ba4b39209cf7d70b6d34a8d34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const eventLoopLag = (() => { | |
let count = 0; | |
let avg: number | null = null; | |
let max: number | null = null; | |
const avgMax = () => `Average: ${avg?.toFixed(1)} ms - Max: ${max?.toFixed(1)} ms`; | |
const track = (countAverages = false, printAveragesEvery = 1_000_000, printImmediatelyThresholdMs = 10) => { | |
const start = performance.now(); | |
setImmediate(() => { | |
const delay = performance.now() - start; | |
if (delay >= printImmediatelyThresholdMs) { | |
console.log(`Event Loop LAG: ${delay.toFixed(1)} ms - previously -> ${avgMax()}`); | |
count = 0; | |
avg = null; | |
max = null; | |
} else if (countAverages) { | |
count++; | |
avg = avg ? (avg + delay) / 2 : delay; | |
max = max ? Math.max(max, delay) : delay; | |
} | |
if (count >= printAveragesEvery) { | |
console.log(`${count} counts. ${avgMax()}`); | |
count = 0; | |
avg = null; | |
max = null; | |
} | |
track(countAverages, printAveragesEvery, printImmediatelyThresholdMs); | |
}); | |
}; | |
return track; | |
})(); | |
eventLoopLag(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as process from 'process'; | |
import { setFlagsFromString } from 'v8'; | |
import { runInNewContext } from 'vm'; | |
setInterval(() => { | |
const memoryUsage = process.memoryUsage(); | |
console.table([{ | |
'RSS (Resident Set Size)': `${Math.round(memoryUsage.rss / 1024 / 1024)} MB`, | |
'Heap Total': `${Math.round(memoryUsage.heapTotal / 1024 / 1024)} MB`, | |
'Heap Used': `${Math.round(memoryUsage.heapUsed / 1024 / 1024)} MB`, | |
'External': `${Math.round(memoryUsage.external / 1024 / 1024)} MB`, | |
'Array Buffers': `${Math.round(memoryUsage.arrayBuffers / 1024 / 1024)} MB`, | |
}]); | |
// call gc | |
setFlagsFromString('--expose_gc'); | |
const gc = runInNewContext('gc'); // nocommit | |
gc(); | |
}, 500).unref(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment