Performance API
The identifier read-only property of the PerformanceContainerTiming interface returns the value of the containertiming attribute on the container's root element.
This can be used to determine the container root that each entry belongs to, which is useful if multiple containers are being reported at the same time. This approach is easier than comparing PerformanceContainerTiming.rootElement references.
Value
A string.
Examples
Using identifier to distinguish containers
This example shows how you can determine the affected container using identifier.
HTML
First we define two elements that are marked as container roots with the containertiming attribute, identified as "hero" and "product-list", respectively.
<section containertiming="hero">
<h2>Hero content</h2>
</section>
<div containertiming="product-list">
<p>Product list content</p>
</div>
Note that there is also hidden HTML (and code) for displaying log information.
<pre id="log"></pre>
#log {
height: 150px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
JavaScript
The following code first checks whether the browser supports "container" entries: if not, it logs that the feature is not supported.
It then creates a PerformanceObserver that logs the identifier and PerformanceEntry.startTime of each entry, allowing the two containers to be told apart.
if (PerformanceObserver.supportedEntryTypes.includes("container")) {
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
log(`${entry.identifier} painted at ${entry.startTime.toFixed(1)}`);
}
});
observer.observe({ type: "container", buffered: true });
} else {
log("This feature is not supported by your browser.");
}
Result
The following log shows the identifier and startTime reported for each of the two containers.
Note that the start time is the same in this case, as they are painted in the same frame.
Using identifier to distinguish containers
Specifications
Browser compatibility
See also
containertimingHTML attribute
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
