Skip to content

Instantly share code, notes, and snippets.

@arbaouimehdi
Created March 17, 2024 14:48
Show Gist options
  • Save arbaouimehdi/db36e0aa89b600cce156c35d67093ae2 to your computer and use it in GitHub Desktop.
Save arbaouimehdi/db36e0aa89b600cce156c35d67093ae2 to your computer and use it in GitHub Desktop.
Measures the duration of a 2-second simulated operation after page `load` using `performance.now()`
// Function to simulate
// a long-running operation
function longRunningOperation() {
// Simulate a 2-second operation
return new Promise((resolve) => {
setTimeout(() => {
console.log(
"Operation completed"
);
resolve();
}, 2000);
});
}
// Measure the time taken
// for the long-running
// operation after the `load`
window.addEventListener(
"load",
async () => {
console.log("Page loaded");
// Start measuring time
const startTime = performance.now();
// Perform the long-running operation
await longRunningOperation();
// Stop measuring time
const endTime = performance.now();
// Calculate the duration
// of the operation
const duration =
endTime - startTime;
console.log(
`Operation took ${duration} milliseconds`
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment