> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# PerformanceObserver

APISummary
`PerformanceObserver` 用于观察性能事件，并在 [Lynx Engine](/next/zh/guide/spec.md#engine) 产生性能事件 ([`PerformanceEntry`](/next/zh/api/lynx-api/performance-api/performance-entry.md)) 时收到通知。
它必须通过 [`lynx.performance.createObserver()`](/next/zh/api/lynx-api/lynx/lynx-performance/create-observer.md) 创建，并在创建时绑定一个回调函数用于处理接收到的性能事件。

## 实例方法

[`PerformanceObserver.observe()`](/next/zh/api/lynx-api/performance-api/performance-observer/observe.md)

指定要观察的性能事件类型集合并开始观察。

[`PerformanceObserver.disconnect()`](/next/zh/api/lynx-api/performance-api/performance-observer/disconnect.md)

停止接收性能事件。

## 示例

### 创建一个 PerformanceObserver

该示例展示了如何创建一个 `PerformanceObserver` 并监听 [`metric.fcp`](/next/zh/api/lynx-api/performance-api/performance-entry/metric-fcp-entry.md) 和 [`pipeline`](/next/zh/api/lynx-api/performance-api/performance-entry/pipeline-entry.md) 事件。

**下面是一个示例: performance-api**

**Entry:** `src/simple-observe`
**Bundle:** `dist/simple-observe.lynx.bundle`

```tsx {11-30}
import * as React from "@lynx-js/react";
import { root, useMemo, useState } from "@lynx-js/react";
import type { PerformanceEntry } from "@lynx-js/types";
import "../common/common.css";
import "./index.css";

export default function SimpleObserveExample(this: any) {
  const [receivedEntries, setReceivedEntries] = useState<Set<string>>(new Set<string>());

  useMemo(() => {
    "background-only";
    // 1. Create a performance observer.
    const observer = lynx.performance.createObserver((entry: PerformanceEntry) => {
      // 3. process "metric.fsp" and "pipeline"
      let entryType: string | undefined;
      if (entry.entryType == "metric" && entry.name == "fsp") {
        entryType = "MetricFspEntry";
      } else if (entry.entryType == "pipeline") {
        entryType = "PipelineEntry";
      }
      if (entryType) {
        setReceivedEntries(prevEntries => {
          const newEntries = new Set(prevEntries);
          newEntries.add(entryType);
          return newEntries;
        });
      }
    });
    // 2. register to listen to the "metric.fsp" and "pipeline" event.
    observer.observe(["metric.fsp", "pipeline"]);
  }, []);

  return (
    <view className="container">
      <view className="title-container">
        <text className="title">Hello PerformanceObserver</text>
      </view>
      <view className="card">
        <text className="section-title">Received PerformanceEntry:</text>
        <view className="entries-container">
          {Array.from(receivedEntries).map((entryName, index) => (
            <text
              key={index}
              className="entry-item"
            >
              {entryName}
            </text>
          ))}
        </view>
      </view>
    </view>
  );
}

root.render(<SimpleObserveExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.8.0



## 兼容性

**Compatibility Table**

**Query:** `lynx-api.performance-api.performance-observer`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/performance-api/performance-observer.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| performance-observer | Yes (3.1) | Yes (3.4) | Yes (3.1) | Yes (3.7) | Yes (3.7) | No |

