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

# PerformanceObserver: observe() method

APISummary
Specifies types of performance events to observe and starts observing. The callback function set when creating the `PerformanceObserver` will be invoked when a matching
[`PerformanceEntry`](/next/api/lynx-api/performance-api/performance-entry.md) is received.

## Syntax

```ts
observe(entryIdentifiers: string[]): void
```

### Parameters

#### entryIdentifiers

```ts
entryIdentifiers: string[];
```

A string list of identifiers for the `PerformanceEntry` to be observed.

You can use **entryType** or **entryType.name** as identifiers. For example, you can use `"metric"` to listen to performance events for the entire metric type,
including [`MetricFcpEntry`](/next/api/lynx-api/performance-api/performance-entry/metric-fcp-entry.md) and [`MetricActualFmpEntry`](/next/api/lynx-api/performance-api/performance-entry/metric-actual-fmp-entry.md). Alternatively, you can use `"metric.fcp"` to listen only to the `MetricFcpEntry` performance event.

Refer to [`PerformanceEntry.entryType`](/next/api/lynx-api/performance-api/performance-entry.md#entrytype) and [`PerformanceEntry.name`](/next/api/lynx-api/performance-api/performance-entry.md#name) for available types
and names. Unrecognized types will be ignored. If no valid types are found, `observe()` will not function.

:::tip
It is recommended to call this method during initialization, such as in the constructor of a root component or within the [`useMemo`](/next/api/react/Hooks/useMemo.md) Hook,
to ensure it is called as early as possible when the component or page loads, to avoid missing any data.
:::

### Return Value

None (`undefined`)

## Examples

### Observing all performance events under certain entryTypes

This example creates a `PerformanceObserver` that observes the entire `PerformanceEntry` of types through `["metric", "pipeline"]`.

**This is an example below: performance-api**

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

```tsx {12-37}
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 InitLynxviewEntryExample(this: any) {
  const [receivedTypeEntries, setReceivedTypeEntries] = useState<Set<string>>(new Set<string>());
  const [receivedSpecificEntries, setReceivedSpecificEntries] = useState<Set<string>>(new Set<string>());

  function observerAllEntryTypePerformanceEntry() {
    "background-only";
    // 1. Create a performance observer.
    const observer = lynx.performance.createObserver((entry: PerformanceEntry) => {
      console.log(JSON.stringify(entry));
      // 3. process "metric.fcp" and "pipeline"
      let entryType: string | undefined;
      if (entry.entryType == "metric") {
        if (entry.name == "fcp") {
          entryType = "MetricFcpEntry";
        } else if (entry.name == "actualFmp") {
          entryType = "MetricActualFmpEntry";
        }
      } else if (entry.entryType == "pipeline") {
        entryType = "PipelineEntry";
      }
      if (entryType) {
        setReceivedTypeEntries(prevEntries => {
          const newEntries = new Set(prevEntries);
          newEntries.add(entryType);
          return newEntries;
        });
      }
    });
    // 2. register to listen to the "metric" and "pipeline" event.
    observer.observe(["metric", "pipeline"]);
  }

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

  useMemo(() => {
    observerAllEntryTypePerformanceEntry();
    observeSpecificPerformanceEntry();
  }, []);

  return (
    <view className="container">
      <view className="title-container">
        <text className="title">Hello PerformanceObserver::observe()</text>
      </view>
      <view className="card">
        <text className="section-title">Receive All EntryType Events:</text>
        <view className="entries-container">
          {Array.from(receivedTypeEntries).map((entryName, index) => (
            <text
              key={index}
              className="entry-item"
            >
              {entryName}
            </text>
          ))}
        </view>
      </view>
      <view className="card">
        <text className="section-title">Receive Specific EntryType Events:</text>
        <view className="entries-container">
          {Array.from(receivedSpecificEntries).map((entryName, index) => (
            <text
              key={index}
              className="entry-item"
            >
              {entryName}
            </text>
          ))}
        </view>
      </view>
    </view>
  );
}

root.render(<InitLynxviewEntryExample />);

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

```

v0.8.0



### Observing specific performance events

This example creates a `PerformanceObserver` that observes `MetricFcpEntry` and the entire `PerformanceEntry` of type `pipeline` through `["metric.fcp", "pipeline"]`.

**This is an example below: performance-api**

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

```tsx {42-60}
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 InitLynxviewEntryExample(this: any) {
  const [receivedTypeEntries, setReceivedTypeEntries] = useState<Set<string>>(new Set<string>());
  const [receivedSpecificEntries, setReceivedSpecificEntries] = useState<Set<string>>(new Set<string>());

  function observerAllEntryTypePerformanceEntry() {
    "background-only";
    // 1. Create a performance observer.
    const observer = lynx.performance.createObserver((entry: PerformanceEntry) => {
      console.log(JSON.stringify(entry));
      // 3. process "metric.fcp" and "pipeline"
      let entryType: string | undefined;
      if (entry.entryType == "metric") {
        if (entry.name == "fcp") {
          entryType = "MetricFcpEntry";
        } else if (entry.name == "actualFmp") {
          entryType = "MetricActualFmpEntry";
        }
      } else if (entry.entryType == "pipeline") {
        entryType = "PipelineEntry";
      }
      if (entryType) {
        setReceivedTypeEntries(prevEntries => {
          const newEntries = new Set(prevEntries);
          newEntries.add(entryType);
          return newEntries;
        });
      }
    });
    // 2. register to listen to the "metric" and "pipeline" event.
    observer.observe(["metric", "pipeline"]);
  }

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

  useMemo(() => {
    observerAllEntryTypePerformanceEntry();
    observeSpecificPerformanceEntry();
  }, []);

  return (
    <view className="container">
      <view className="title-container">
        <text className="title">Hello PerformanceObserver::observe()</text>
      </view>
      <view className="card">
        <text className="section-title">Receive All EntryType Events:</text>
        <view className="entries-container">
          {Array.from(receivedTypeEntries).map((entryName, index) => (
            <text
              key={index}
              className="entry-item"
            >
              {entryName}
            </text>
          ))}
        </view>
      </view>
      <view className="card">
        <text className="section-title">Receive Specific EntryType Events:</text>
        <view className="entries-container">
          {Array.from(receivedSpecificEntries).map((entryName, index) => (
            <text
              key={index}
              className="entry-item"
            >
              {entryName}
            </text>
          ))}
        </view>
      </view>
    </view>
  );
}

root.render(<InitLynxviewEntryExample />);

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

```

v0.8.0



## Compatibility

**Compatibility Table**

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

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

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

