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


# Performance API

## Concepts and usage

Measuring and analyzing performance metrics is critical to ensure the speed and user experience of Lynx applications. Similar to the [Web standard](https://developer.mozilla.org/en-US/docs/Web/API/Performance), Lynx provides the **Performance API** as a standardized interface to measure Lynx app performance, helping developers monitor comprehensive metrics from initialization through rendering completion.

![](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/performance-api-v4.png)
### Performance entry types

The core of the Performance API is the **[`PerformanceEntry`](/next/api/lynx-api/performance-api/performance-entry.md)** object, which is the fundamental data structure describing a performance event. Each PerformanceEntry has the following basic properties:

- `entryType`: The type of performance event (e.g., `init`, `metric`, `pipeline`, `resource`)
- `name`: The specific name of the performance event
- Other type-specific properties and timestamps

Based on the `entryType` property, the Performance API supports four main categories of performance events:

- **Initialization events (`init`)**
  - [`InitContainerEntry`](/next/api/lynx-api/performance-api/performance-entry/init-container-entry.md): Container initialization performance event
  - [`InitLynxviewEntry`](/next/api/lynx-api/performance-api/performance-entry/init-lynxview-entry.md): LynxView initialization performance event
  - [`InitBackgroundRuntimeEntry`](/next/api/lynx-api/performance-api/performance-entry/init-background-runtime-entry.md): Background runtime initialization performance event

- **Metric events (`metric`)**
  - [`MetricFcpEntry`](/next/api/lynx-api/performance-api/performance-entry/metric-fcp-entry.md): FCP (First Contentful Paint)
  - [`MetricActualFmpEntry`](/next/api/lynx-api/performance-api/performance-entry/metric-actual-fmp-entry.md): ActualFMP (Actual First Meaningful Paint)

- **Rendering pipeline events (`pipeline`)**
  - [`PipelineEntry`](/next/api/lynx-api/performance-api/performance-entry/pipeline-entry.md): Performance event for [flagged rendering pipelines](/next/guide/performance/monitor-performance/timing-flag.md)
  - [`LoadBundleEntry`](/next/api/lynx-api/performance-api/performance-entry/load-bundle-entry.md): Performance event for [TemplateBundle](/next/api/lynx-native-api/template-bundle.md) loading and first-frame rendering
  - [`ReloadBundleEntry`](/next/api/lynx-api/performance-api/performance-entry/reload-bundle-entry.md): Performance event for reloading [TemplateBundle](/next/api/lynx-native-api/template-bundle.md)

- **Resource loading events (`resource`)**
  - [`LazyBundleEntry`](/next/api/lynx-api/performance-api/performance-entry/lazy-bundle-entry.md): Performance event for lazy-loaded [TemplateBundle](/next/api/lynx-native-api/template-bundle.md)

### Getting performance data

#### Front-end developers

To consume performance events, the Performance API provides **PerformanceObserver**, which notifies you when the **Lynx Engine** produces performance events (`PerformanceEntry`).

The PerformanceObserver object offers the `observe` method to start observing and the `disconnect` method to stop. With them, you can observe by `entryType` (all entries of a type) or `entryType.name` (a specific kind of entry).

To prevent missing events due to late registration, register observations as early as possible.

- **Component dependencies**
  - `@lynx-js/react` >= 0.107.0

- **Registration timing**
  - Class components: register in the **constructor**
  - Function components: register in **useMemo**

The following example shows how to create a `PerformanceObserver` and observe [`metric.fcp`](/next/api/lynx-api/performance-api/performance-entry/metric-fcp-entry.md) and [`pipeline`](/next/api/lynx-api/performance-api/performance-entry/pipeline-entry.md) events.

**This is an example below: 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



#### Client developers

On the client side, performance events sent by the Performance API are delivered via the `onPerformanceEvent(PerformanceEntry entry)` callback on LynxViewClient on an **asynchronous thread**. It is therefore not recommended to perform any UI-related operations inside this callback.

iOSAndroidHarmonyOS

```java
import com.lynx.tasm.LynxViewClientV2;
import com.lynx.tasm.performance.performanceobserver.PerformanceEntry;
import com.lynx.tasm.performance.performanceobserver.MetricFcpEntry;

public class CustomLynxViewClient extends LynxViewClientV2 {
  @Override
  public void onPerformanceEvent(@NonNull PerformanceEntry entry) {
    // If you need to consume raw data directly, convert to HashMap
    Log.d(entry.toHashMap());
    // If you need to consume a specific type, cast and use inner fields
    if (entry.entryType.equals("metric") && entry.name.equals("fcp")) {
      MetricFcpEntry fcpEntry = (MetricFcpEntry) entry;
      Log.d("lynxFcp is %s", fcpEntry.lynxFcp.duration);
    }
  }
}
```

```objc
#import "LynxViewClient.h"
#import "LynxMetricFcpEntry.h"

@interface CustomLynxViewClient : NSObject <LynxViewLifecycleV2>
@end
@implementation

- (void)onPerformanceEvent:(LynxPerformanceEntry*)entry {
  // If you need to consume raw data directly, convert to NSDictionary*
  NSLog(entry.toDictionary);
  // If you need to consume a specific type, cast and use inner fields
  if ([entry.entryType isEqual: @"metric"] && [entry.name isEqual: @"fcp"]) {
    MetricFcpEntry* fcpEntry = (LynxMetricFcpEntry *) entry;
    NSLog(@"lynxFcp is %@", fcpEntry.lynxFcp.duration);
  }
}
@end

```

```typescript
import { LynxViewClient, PerformanceEntry, MetricFcpEntry } from '@lynx/lynx';

export class CustomLynxViewClient extends LynxViewClient {
  public onPerformanceEvent(entry: PerformanceEntry): void {
    // If you need to consume raw data directly, convert to Record
    Log.d(entry.Record);
    // If you need to consume a specific type, cast and use inner fields
    if (entry.entryType == 'metric' && entry.name == 'fcp') {
      let fcpEntry: MetricFcpEntry = entry as MetricFcpEntry;
      Log.d('lynxFcp is %s', fcpEntry.lynxFcp.duration);
    }
  }
}
```

## [Capture specific rendering pipelines](/next/guide/performance/monitor-performance/timing-flag.md)

A [rendering pipeline](/next/guide/spec.md#lynx-pipeline) is the complete process from triggering the rendering to displaying it on the screen. If you care about the rendering performance of certain key components, set the component’s `__lynx_timing_flag` property to mark the pipeline it belongs to and monitor its performance.

When the flagged rendering pipeline completes and the screen is refreshed, a [`PipelineEntry`](/next/api/lynx-api/performance-api/performance-entry/pipeline-entry.md) event is generated. You can retrieve it via [`PerformanceObserver`](/next/api/lynx-api/performance-api/performance-observer.md).

## Build custom performance metrics

Different business goals imply different metrics to focus on. Your use of the Performance API need not be limited to built-in Lynx metrics—you can flexibly combine timestamps from different `PerformanceEntry`s at key points to construct a metric suite tailored to your application.

For example, if you want to measure the latency from the end of first-frame rendering to the first important data update, you can combine `LoadBundleEntry` and `PipelineEntry` to compute a custom metric `waitingDuration` as shown below. It helps monitor the speed of network requests, file IO, etc., and pinpoint causes of performance regressions.

![waiting duration](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/lynx-website/assets/doc/custom-metric-waitingduration.png)

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

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

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

export default function CreateCustomPerformanceMetricExample(this: any) {
  const [loadBundleEnd, setLoadBundleEnd] = useState<number>(0);
  const [pipelineStart, setPipelineStart] = useState<number>(0);
  const [waitingDuration, setWaitingDuration] = useState<number>(0);
  const [state, setState] = useState({
    importantElement: "Loading",
    __lynx_timing_flag: "",
  });

  const loadBundleEndRef = useRef(0);
  const observerRef = useRef<PerformanceObserver>();

  function observerPerformanceEntry() {
    "background-only";
    // 1. Create a performance observer.
    const observer = lynx.performance.createObserver((entry: PerformanceEntry) => {
      console.log(JSON.stringify(entry));
      if (entry.entryType == "pipeline" && entry.name == "loadBundle") {
        // 3. Received "pipeline.loadBundle" event.
        const LoadBundleEntry = entry as LoadBundleEntry;
        setLoadBundleEnd(LoadBundleEntry.loadBundleEnd);
      } else if (entry.entryType == "pipeline") {
        // 4. calculate waitingDuration
        const pipelineEntry = entry as PipelineEntry;
        if (pipelineEntry.identifier == "important-update") {
          setPipelineStart(pipelineEntry.pipelineStart);
          setWaitingDuration(pipelineEntry.pipelineStart - loadBundleEndRef.current);
          observer.disconnect();
        }
      }
    });
    // 2. register to listen to the "pipeline" event.
    observer.observe(["init", "pipeline"]);
    observerRef.current = observer;
  }

  useMemo(() => {
    observerPerformanceEntry();
  }, []);

  useEffect(() => {
    loadBundleEndRef.current = loadBundleEnd;
  }, [loadBundleEnd]);

  useEffect(() => {
    const timeout = setTimeout(() => {
      setState(() => ({
        __lynx_timing_flag: "important-update",
        importantElement: "Display",
      }));
    }, 3000);

    return () => {
      clearTimeout(timeout);
    };
  });

  return (
    <view className="container">
      <text className="title">Create Your Performance Metric</text>
      <view className="card">
        <text className="entry highlight" __lynx_timing_flag={state.__lynx_timing_flag}>
          🎯 {state.importantElement} important element
        </text>
        <text className="entry">⏱️ loadBundleEnd: {loadBundleEnd} ms</text>
        <text className="entry">⏱️ updateTime: {pipelineStart} ms</text>
        <text className="entry">⏳ waitingDuration: updateTime - loadBundleEnd = {waitingDuration} ms</text>
      </view>
    </view>
  );
}

root.render(<CreateCustomPerformanceMetricExample />);

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

```

v0.8.0



## Best practices

### 1. Register observers early

To avoid missing events due to late registration, register observations as early as possible:

- Register in the class component `constructor`, or in `useMemo` for function components

### 2. Choose an appropriate observe scope

Select the observe scope based on your needs:

```typescript
// Observe all metric events
observer.observe(['metric']);

// Observe only FCP metric events
observer.observe(['metric.fcp']);

// Observe multiple specific events
observer.observe(['metric.fcp', 'pipeline.loadBundle']);
```

### 3. Clean up promptly

When a component unmounts or observation is no longer needed, call `disconnect()` to release resources:

```typescript
useEffect(() => {
  return () => {
    if (observer) {
      observer.disconnect();
    }
  };
}, []);
```

## FAQ

### Q1: If my page uses [_Instant First-Frame Rendering (IFR)_](/next/guide/interaction/ifr.md), do I still need to mark a Timing Flag? \{#q1-if-my-page-uses-instant-first-frame-rendering-ifr-do-i-still-need-to-mark-a-timing-flag}

For pages with [_Instant First-Frame Rendering (IFR)_](/next/guide/interaction/ifr.md), you generally do not need to manually mark a Timing Flag. However, if you do, you will still receive a `LoadBundleEntry` with an `identifier` value equal to the TimingFlag you marked.

### Q2: My page uses [_Instant First-Frame Rendering (IFR)_](/next/guide/interaction/ifr.md). How can I get the ActualFMP performance metric? \{#q2-my-page-uses-instant-first-frame-rendering-ifr-how-can-i-get-the-actualfmp-performance-metric}

In scenarios with [_Instant First-Frame Rendering (IFR)_](/next/guide/interaction/ifr.md), you generally do not need to focus on the ActualFMP performance metric; you only need to pay attention to the FCP metric. The FCP metric can be obtained directly through `MetricFcpEntry`.

### Q3: Can I get the rendering time of lazy-loaded components?

Yes. Marking Timing Flags on lazy-loaded components works the same as on the main page; you can use the Timing Flag inside the lazy-loaded component:

```typescript
export default function MyLazyBundle() {
  return (
    <view className="root">
      <text className="text", __lynx_timing_flag="dynamic_render" >Hello, This is a Lazy Bundle!</text>
    </view>
  );
}
```

```typescript
export default function app() {
  return (
    <view className="container">
      <text className="title">Hello LazyBundleEntry~</text>
      <Suspense fallback={<text className="sub-text">Loading...</text>}>
        <MyLazyBundle />
      </Suspense>
      <ScrollItem title={entryName} value={lazyBundleEntry} />
    </view>
  );
}
```

This method can be used to obtain the timestamp of the dynamic component rendering phase. For information about the time consumption of the dynamic component resource loading phase, refer to [`LazyBundleEntry`](/next/api/lynx-api/performance-api/performance-entry/lazy-bundle-entry.md).

### Q4: Are Performance API callbacks related to other Lynx lifecycles?

Performance API triggers its callbacks after all rendering pipeline timestamps are captured—i.e., after pixels are on screen—and has no strict ordering relationship with other Lynx lifecycles.

### Q5: Which is triggered faster, a setState callback or a Performance API callback?

See Q4: there is no defined ordering.

### Q6: Can Timing Flags be reused?

No. If two identical Timing Flags are injected, Performance API fires on the first one it detects and will not fire again for the second.

### Q7: What happens if the client calls ReloadTemplate, calls LoadTemplate multiple times, or the front end calls reload?

**Before Lynx 3.4**: Performance API state resets; LoadBundleEntry will be triggered again with timestamps from this template load. If front-end reloads but the page has no UI to redraw, no callback is received.

**After Lynx 3.4**: Multiple LoadTemplate calls yield multiple LoadBundleEntry events; client ReloadTemplate and front-end reload yield ReloadBundleEntry.

### Q8: Why don’t I receive the LoadBundleEntry callback?

Possible causes:

1. First-frame rendering produced no pixels, e.g., LynxView width/height is 0, LynxView is off-screen, or LynxView wasn’t added to the window.
2. The observer was registered too late; try registering earlier:
   - register in the class constructor or `useMemo`

### Q9: I used an Timing Flag—why don’t I receive PipelineEntry callbacks?

Possible causes:

1. Observer registered too late (see Q8)
2. Timing Flag is set on a Component while the `removeComponentElement` switch is enabled, making the Timing layer unable to detect the flag
3. Timing Flag is ineffective on tags with no UI nodes, such as `<inline text/>`, `<block/>`, `<template/>`
4. The same Timing Flag does not trigger multiple callbacks (see Q6)

### Q10: How do I verify that Timing Flag integration works?

You can validate via local Trace, or by registering an observer and checking whether callbacks fire.

### Q11: My ActualFMP metric is particularly large—why?

Check whether preloading is in effect; with preloading, the starting point of ActualFMP can be very early.

### Q12: Error “OnPipelineStart arg count must == 1”

To fix this, upgrade `@lynx-js/react` to version 0.107.1 or higher.
