大网数据广告活动
Web 标准与前端开发

PerformanceElementTiming

· 52CE 我爱测

The **`PerformanceElementTiming`** interface contains render timing information for image and text node elements the developer annotated with an `elementtiming` attribute for observation.

Performance API

The PerformanceElementTiming interface contains render timing information for image and text node elements the developer annotated with an elementtiming attribute for observation.

PerformanceElementTiming inherits from PerformanceEntry.

Instance properties

This interface directly defines the following properties:

  • PerformanceElementTiming.element
    • : An Element representing the element we are returning information about.
  • PerformanceElementTiming.id
    • : A string which is the id of the element.
  • PerformanceElementTiming.identifier
    • : A string which is the value of the elementtiming attribute on the element.
  • PerformanceElementTiming.intersectionRect
    • : A DOMRectReadOnly which is the rectangle of the element within the viewport.
  • PerformanceElementTiming.loadTime
    • : A DOMHighResTimeStamp with the loadTime of the element.
  • PerformanceElementTiming.naturalHeight
    • : An unsigned 32-bit integer (unsigned long) which is the intrinsic height of the image if this is applied to an image, 0 for text.
  • PerformanceElementTiming.naturalWidth
    • : An unsigned 32-bit integer (unsigned long) which is the intrinsic width of the image if this is applied to an image, 0 for text.
  • PerformanceElementTiming.paintTime
    • : Returns the DOMHighResTimeStamp when the rendering phase ended and the paint phase started.
  • PerformanceElementTiming.presentationTime
    • : Returns the DOMHighResTimeStamp when the element was actually drawn on the screen.
  • PerformanceElementTiming.renderTime
    • : A DOMHighResTimeStamp with the renderTime of the element.
  • PerformanceElementTiming.url
    • : A string which is the initial URL of the resources request for images, 0 for text.

It also extends the following PerformanceEntry properties, qualifying and constraining them as described:

  • PerformanceEntry.duration
    • : Always returns 0 as duration does not apply to this interface.
  • PerformanceEntry.entryType
    • : Always returns "element".
  • PerformanceEntry.name
    • : Returns "image-paint" for images and "text-paint" for text.
  • PerformanceEntry.startTime
    • : Returns the value of this entry's PerformanceElementTiming.renderTime if it is not 0, otherwise the value of this entry's PerformanceElementTiming.loadTime.

Instance methods

  • PerformanceElementTiming.toJSON()
    • : Returns a JSON-serializable plain object representing the PerformanceElementTiming object. Automatically called by JSON.stringify().

Description

The aim of the Element Timing API is to give web developers or analytics tools the ability to measure rendering timestamps of critical elements on a page.

The API supports timing information on the following elements:

  • img elements,
  • image elements inside an svg,
  • poster images of video elements,
  • elements which have a contentful background-image property with a URL value for a resource that is actually available, and
  • groups of text nodes, such as a p.

The author flags an element for observation by adding the elementtiming attribute on the element.

Examples

Observing render time of specific elements

In this example two elements are being observed by adding the elementtiming attribute. A PerformanceObserver is registered to get all performance entries of type "element" and the buffered flag is used to access data from before observer creation.

<img src="image.jpg" elementtiming="big-image" />
<p elementtiming="text" id="text-id">text here</p>
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    console.log(entry);
  });
});
observer.observe({ type: "element", buffered: true });

Two entries will be output to the console. The first containing details of the image, the second with details of the text node.

Observing separate paint and presentation timings

The paintTime and presentationTime properties enable you to retrieve specific timings for the paint phase starting and the element being drawn on the screen. The paintTime is broadly interoperable, whereas the presentationTime is implementation-dependent.

This example uses a PerformanceObserver to observe all performance entries of type "element" (remember that, to be observed, elements need to have elementtiming attributes set on them). We check for paintTime and presentationTime support and retrieve those values if they are available. In non-supporting browsers, the code retrieves the renderTime or loadTime, depending on what is supported.

const observer = new PerformanceObserver((list) => {
  const entries = list.getEntries();
  entries.forEach((entry) => {
    if (entry.presentationTime) {
      console.log(
        "Element paintTime:",
        entry.paintTime,
        "Element presentationTime:",
        entry.presentationTime,
      );
    } else if (entry.paintTime) {
      console.log("Element paintTime:", entry.paintTime);
    } else if (entry.renderTime) {
      console.log("Element renderTime:", entry.renderTime);
    } else {
      console.log("Element loadTime", entry.loadTime);
    }
  });
});
observer.observe({ type: "element", buffered: true });

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。