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

# Profiling performance

Find out what makes your app slow. Record a trace of the running app in Lynx DevTool, mark your own code in it with `LynxPerformance`, and watch AngularLynx's render counters with `LynxDevTools`.

## Record a trace

A **trace** is a timeline of what the app did during a recording, on every thread. You record one with [Lynx DevTool](/guide/glossary.md#lynx-devtool).

1. Connect Lynx Explorer to Lynx DevTool, as in [Debugging](/guide/debugging.md#inspect-the-app-with-lynx-devtool).
2. In Lynx DevTool, choose **Trace** from the top bar, and pick your app.
3. Press **Start**, use the slow part of your app, then press **Stop**.
4. Press **Analyze** to look at the timeline. **Download** saves the trace as a file, which you can open in [Perfetto](https://ui.perfetto.dev/).

To trace the app's launch, use **Startup Trace** in the top bar instead. Lynx's [trace guide](https://lynxjs.org/guide/devtool/trace) covers both.

:::info Traces need Lynx Explorer
Only development builds of the Lynx engine can record traces. Lynx Explorer has one. The apps `--platform` builds, with `ng build` or `ng serve`, use the release engine, which can't.
:::

## Mark your own code in the trace

`LynxPerformance` adds named sections and marks to the trace, so you can find your own code in the timeline:

```typescript title="src/app/feed.ts"
import { Component, inject, signal } from '@angular/core';
import { LYNX_ELEMENTS, LynxPerformance } from '@blotch/angular-lynx';

@Component({
  selector: 'app-feed',
  imports: [LYNX_ELEMENTS],
  template: `
    <view (bindtap)="sort()">
      <text>Sort {{ items().length }} items</text>
    </view>
  `,
})
export class Feed {
  readonly items = signal<readonly number[]>([3, 1, 2]);
  readonly #perf = inject(LynxPerformance);

  sort(): void {
    this.#perf.profileStart('Feed::sort', {
      args: { count: String(this.items().length) },
    });
    this.items.update((list) => [...list].sort((a, b) => a - b));
    this.#perf.profileEnd();

    this.#perf.profileMark('Feed::sorted');
  }
}
```

- **`profileStart(name)` and `profileEnd()`** wrap a section. Sections nest: each `profileEnd()` closes the most recent `profileStart()`.
- **`profileMark(name)`** adds a single point in time.
- **`args`** attaches values you can read in the trace. They must be strings: Lynx drops any other value, so convert numbers with `String()`.
- **`isRecording()`** tells you whether a trace is recording. Check it before you compute expensive `args`.
- **`profileFlowId()`** returns an ID. Pass it as `flowId` to `profileStart()` or `profileMark()` to link sections and marks, for example across threads. `flowIds` links one to several flows.

On a device, the calls do nothing while no trace is recording. Outside Lynx, e.g. in tests, they do nothing at all.

### In the web preview

The web preview has no Lynx DevTool trace. There, `LynxPerformance` writes to the browser's performance timeline instead: a section becomes a User Timing measure, and a mark a User Timing mark. Record in the Performance panel of Chrome DevTools, and find them in the **Timings** track. `isRecording()` always returns `true` there.

## Watch render counters

`LynxDevTools` counts AngularLynx's rendering work and exposes the counts as a signal. Put them on screen to spot extra work, such as a tap that re-renders far more than you expect:

```typescript title="src/app/debug-overlay.ts"
import { Component, inject } from '@angular/core';
import { LYNX_ELEMENTS, LynxDevTools } from '@blotch/angular-lynx';

@Component({
  selector: 'app-debug-overlay',
  imports: [LYNX_ELEMENTS],
  template: `
    <view>
      <text>Change detection runs: {{ devtools.stats().cdCycles }}</text>
      <text>
        Elements: +{{ devtools.stats().elementCreated }} / -{{
          devtools.stats().elementRemoved
        }}
      </text>
      <text>Flushes: {{ devtools.stats().flushCount }}</text>
      <text>Last run: {{ devtools.stats().lastCdDurationMs }} ms</text>
      <view (bindtap)="devtools.refresh()">
        <text>Refresh</text>
      </view>
      <view (bindtap)="devtools.reset()">
        <text>Reset</text>
      </view>
    </view>
  `,
})
export class DebugOverlay {
  readonly devtools = inject(LynxDevTools);
}
```

| Counter            | What it shows                                                                                   |
| ------------------ | ----------------------------------------------------------------------------------------------- |
| `cdCycles`         | Change detection runs                                                                           |
| `elementCreated`   | Native elements created                                                                         |
| `elementRemoved`   | Native elements removed                                                                         |
| `flushCount`       | Times AngularLynx sent a batch of changes to the screen                                         |
| `lastCdDurationMs` | How long the last change detection run took, in ms, including sending its changes to the screen |

- **`stats()` updates only when you ask.** Call `refresh()` to read the latest counts, or `reset()` to set them back to zero. The counters change on every render, so updating the signal each time would cause renders of its own.
- **`snapshot()`** returns the counts once, without a signal, e.g. to log them.

## Measure startup on a phone

A trace shows what the app does. To time how long it takes to start, mark the moment its first screen is complete with [`reportFullyDrawn()`](/api/angular-lynx/host/fully-drawn.md):

```typescript
effect(() => {
  if (this.items.hasValue()) reportFullyDrawn();
});
```

- **iOS:** record the release app with Instruments' **App Launch** template plus the **Points of Interest** instrument. The launch phases end at the first frame; the `Fully Drawn` point marks the screen being complete.
- **Android:** `adb logcat | grep "Fully drawn"` prints the time from the tap, and [Macrobenchmark](https://developer.android.com/topic/performance/benchmarking/macrobenchmark-overview)'s `StartupTimingMetric` reads it as time to full display.

Measure a release build (`ng build --platform`) on a real phone. A development build and the simulator are both slower in ways that have nothing to do with your app.

## Profiling in production builds

AngularLynx's own profiling code is removed from production builds:

- **The counters only count in development builds,** such as `ng serve` and `ng build -c development`. In a production build, `stats()` stays at zero.
- **`LynxPerformance` calls stay in,** so you can trace the build you ship, without development mode's extra work in the timeline. They cost little when no trace is recording, and the release Lynx engine records none.

## Related

- [`LynxPerformance` and `LynxDevTools` API](/api/angular-lynx/devtools.md)
- [`reportFullyDrawn()`](/api/angular-lynx/host/fully-drawn.md)
- [Debugging](/guide/debugging.md)
- [Production and development builds](/guide/building-for-production.md#production-and-development-builds)
- [Trace guide](https://lynxjs.org/guide/devtool/trace) on lynxjs.org
