> 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.

# createObserver()

APISummary
创建一个
[`PerformanceObserver`](/next/zh/api/lynx-api/performance-api/performance-observer.md)
对象，该对象将用于接收 Lynx engine 产生的性能事件。

## 语法

```ts
createObserver(callback: PerformanceCallback): PerformanceObserver;
```

## 参数

##### `callback`

处理性能事件的回调函数，类型定义为 `type PerformanceCallback = (entry: PerformanceEntry) => void`。entry 是 [PerformanceEntry](/next/zh/api/lynx-api/performance-api/performance-entry.md) 的子类型。

当 [PerformanceObserver](/next/zh/api/lynx-api/performance-api/performance-observer.md) 接收到性能事件时该回调被触发。

## 返回值

一个 [`PerformanceObserver`](/next/zh/api/lynx-api/performance-api/performance-observer.md) 对象。

## 示例

该示例展示了如何创建一个 `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.lynx.performance.createObserver()`

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

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| createObserver() | Yes (3.1) | Yes (3.4) | Yes (3.1) | Yes (3.5) | Yes (3.5) | No |

