Created
March 17, 2024 14:48
-
-
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()`
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
// 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