// Place at a location in the code that starts login
performance.mark("login-started");
// Place at a location in the code that finishes login
performance.mark("login-finished");User timing / notebook
User timing
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
performance.mark("login-started", {
startTime: 12.5,
detail: { htmlElement: myElement.id },
});const loginMeasure = performance.measure(
"login-duration",
"login-started",
"login-finished",
);
console.log(loginMeasure.duration);loginButton.addEventListener("click", (clickEvent) => {
fetch(loginURL).then((data) => {
renderLoggedInUser(data);
const marker = performance.mark("login-finished");
performance.measure("login-click", {
detail: { htmlElement: myElement.id },
start: clickEvent.timeStamp,
end: marker.startTime,
});
});
});function perfObserver(list, observer) {
list.getEntries().forEach((entry) => {
if (entry.entryType === "mark") {
console.log(`${entry.name}'s startTime: ${entry.startTime}`);
}
if (entry.entryType === "measure") {
console.log(`${entry.name}'s duration: ${entry.duration}`);
}
});
}
const observer = new PerformanceObserver(perfObserver);
observer.observe({ entryTypes: ["measure", "mark"] });const entries = performance.getEntries();
entries.forEach((entry) => {
if (entry.entryType === "mark") {
console.log(`${entry.name}'s startTime: ${entry.startTime}`);
}
if (entry.entryType === "measure") {
console.log(`${entry.name}'s duration: ${entry.duration}`);
}
});const marks = performance.getEntriesByType("mark");
marks.forEach((entry) => {
console.log(`${entry.name}'s startTime: ${entry.startTime}`);
});
const measures = performance.getEntriesByType("measure");
measures.forEach((entry) => {
console.log(`${entry.name}'s duration: ${entry.duration}`);
});// Log all marks named "debug-marks"
const debugMarks = performance.getEntriesByName("debug-mark", "mark");
debugMarks.forEach((entry) => {
console.log(`${entry.name}'s startTime: ${entry.startTime}`);
});// Clear all marks
performance.clearMarks();
// Removes the marker with the name "myMarker"
performance.clearMarks("myMarker");
// Clear all measures
performance.clearMeasures();
// Removes the measure with the name "myMeasure"
performance.clearMeasures("myMeasure");