Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created March 29, 2021 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmendesneto/c0abc2a0c8d0ced80c94fde7ac73e44e to your computer and use it in GitHub Desktop.
Save willmendesneto/c0abc2a0c8d0ced80c94fde7ac73e44e to your computer and use it in GitHub Desktop.
Using User Timing API in Nodejs via perf_hooks package
const { performance, PerformanceObserver } = require('perf_hooks');
const marker = 'my-marker';
const observer = new PerformanceObserver((list) => {
// Using `console.log()` to display metrics for `my-marker`
// After that, this observer will be disconnected
console.log(list.getEntries().find(i => i.name === marker));
observer.disconnect();
});
observer.observe({ entryTypes: ['measure'] });
// Starting the marker
performance.mark(marker);
setTimeout(() => {
// Calling the marker for comparison
performance.mark(`${marker}-end`);
setTimeout(() => {
performance.measure(marker, marker, `${marker}-end`);
}, 1000);
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment