Profiling, memory and crashes

For AI agents: this page is Markdown at https://squiggle.sh/docs/performance/profiling.md. Every docs page is listed at https://squiggle.sh/llms.txt, and all of them are one file at https://squiggle.sh/llms-full.txt.

When a finding points at slow JS, growing memory or a crash, these commands get you the detail: a CPU profile, heap snapshots to compare, and native crash reports matched to the session.

Profile the JS thread

Terminal
squiggle perf profile --duration 10           # sample the JS thread for 10 secondssquiggle perf profile --duration 10 --out ./checkout.cpuprofile

Use the app while it samples. The profile is saved with the session, and the Performance screen’s Timeline section draws it as a flamegraph: widest first, click a frame to zoom into it. --out also writes a .cpuprofile file that Chrome DevTools can open. It needs a React Native version whose DevTools has the Performance panel. If the app reloads its JS while the profile records, for example with squiggle probe reload, the profile carries on in the reloaded app, so it shows the reload and the new startup as well.

When it finishes, the command lists the functions in your app that took the most time, and on its own line, squiggle (instrumentation): how long Squiggle’s own code in your app took during the capture. Squiggle’s functions are left out of your app’s ranking, and in the saved file each one is named [squiggle] …, so you can tell them apart in DevTools. While a profile or heap snapshot records, Squiggle does less work in your app: it sends what renders less often, and stops timing your hooks. It goes back to normal when the capture ends.

To profile a flow while it runs, ask your agent to start it in the background with run_flow { name: "checkout", waitMs: 0 }, take perf_profile while it runs, then read the flow’s result with intent_wait { runId }. Until the flow ends, your agent can look and measure, but the tools that move the app are refused.

In Studio, Capture profile above the flamegraph records ten seconds of the JS thread into the session and draws the flamegraph when it finishes; if it fails, the reason shows beside the button. The Data section’s Run via CLI section runs these commands for you: Profile (10s), Heap snapshot, Crashes (CLI) and Symbolicate (CLI).

Profile the app’s startup

A profile you start yourself can’t see the app’s launch: by the time Metro lets anything attach to the app, its startup JS has run. So each time a run launches the app, Squiggle keeps the JS thread’s samples as the session’s startup profile. It starts when the app process starts, before the bundle evaluates, so it covers module evaluation, the first render and the first effects. It ends within about two seconds of the app connecting to Squiggle, never partway through a long task: one running at that moment is kept to its end. --startup reads the newest one, with the same summary as a capture, and doesn’t touch the app.

Terminal
squiggle flow checkout --fresh                    # launch the app; its startup is keptsquiggle perf profile --startup                   # the newest launch's startup profilesquiggle perf profile --startup --out ./startup.cpuprofile

Your agent gets the same with perf_profile { startup: true }. It also shows in the Performance screen’s flamegraph list, beside the session’s other profiles. --session <id> reads another session’s.

The summary’s source lines come from the Metro that served the profiled bundle, which the profile’s own frames name, so another project’s Metro running on the same Mac never answers for it. That Metro wins over SQUIGGLE_METRO_URL and metroUrl, which are used only when the frames name no server, and then http://localhost:8081. --metro <url> (metro on perf_profile) names a different one. If that Metro has stopped, the frames keep their bundle positions.

  • It needs a development build on Hermes on an iOS simulator, and a run that launches the app: squiggle flow --fresh, a flow’s launchApp step, or run_flow { fresh: true } with the MCP server’s watch on (the default). A run that launches the app more than once keeps one startup profile per launch, each under that launch’s session. The app drops the window 15 seconds after its process starts, so Squiggle must read it by then: if the dev client spends that long loading the bundle, that launch gets none.
  • In the profiles table its row is named <session>:startup-<time>. The session’s other rows are profiles of single stalls, some cut from the startup profile.
  • When there is none, the command stops with no startup profile stored and says how to get one. The run’s own output (for run_flow, the MCP server’s log) says why it kept none, on its [perf] no startup JS window line.
  • It is sampled at 1,000 samples a second, a tenth of a capture’s rate, so it costs the launch little.

When multiple React Native hosts are registered

A profile can fail with The Tracing domain is unavailable when multiple React Native hosts are registered. React Native refuses to profile an app that has registered more than one debugger page, for as long as that app runs, so there is no page to pick instead: --app can’t change it. A Reanimated worklets runtime registers one with react-native-worklets before 0.11, unless the app runs Hermes V1, and so does every extra React host. The command then fails and lists the app’s pages. When the extra page is a worklets runtime, your agent’s status tool lists perf.profile under unavailable before anyone tries; an extra React host shows only when the profile fails. Update react-native-worklets, or keep the app to one host, then cold-start the app, for example by running a flow with --fresh.

Moving the app to another Metro doesn’t add a host: when a run finds the app on a different Metro, it restarts the app on the right one and says relaunching it on ….

Stalls are profiled for you

While squiggle flow, squiggle record or another command that records a session is attached to a debug build on the simulator, Squiggle starts the profiler as soon as the JS thread has been busy for 200 ms, and stops it when the thread goes idle. The long-task finding for that stall then names the function that was running, and its title says (profiled), because profiling slows the app a little while it runs.

In a React web app the window’s JS is sampled for the whole run, and each stall of 200 ms or more is cut out of it once it ends, as is a cold start’s slow phase after a reload. The session keeps its three longest. A finding names the app function that ran, not the React internals it called, with its source line; an unnamed callback is named by the function it runs in, such as useCartTotals → (anonymous). On a simulator:

  • It needs the Metro server your app loads from. Squiggle uses SQUIGGLE_METRO_URL when it is set, then metroUrl in .squiggle/config.jsonc, then the server your app’s bundle came from, and otherwise the server that serves your app, on any port. Your agent’s status shows which one as metroUrl.
  • Every stall is profiled, and a session keeps the profiles of its three longest, so short stalls while the app starts never use up the ones you are looking for. A capture stops after 10 seconds.
  • Squiggle connects to the app’s debugger once the JS thread is idle and stays connected until the command ends. Metro allows one debugger per app, so React Native DevTools disconnects for that time. Reconnect it afterwards.
  • A stall already under way when the command attaches is not profiled.
  • Your agent’s Squiggle server (run_flow and the other tools) keeps the startup profile and the stalls inside it, but doesn’t profile stalls after startup: it never connects to the app’s debugger, so React Native DevTools stays connected. To profile a later stall, run perf_profile while it happens, or run the flow with squiggle flow.
  • The app’s first stalls after a launch come before the debugger can connect. In a development build on an iOS simulator, Squiggle starts Hermes’s own sampler as the app launches, for at most 15 seconds, and profiles those stalls from it. To turn that off, set SQUIGGLE_JS_STARTUP_PROFILE=0 in the app’s launch environment: in your Xcode scheme, or as SIMCTL_CHILD_SQUIGGLE_JS_STARTUP_PROFILE=0 in the shell that runs squiggle flow --fresh.
  • It needs a React Native version whose DevTools has the Performance panel. On older versions stalls are not profiled.
  • To turn it off, set SQUIGGLE_STALL_PROFILE=0.

Find a memory leak

Terminal
squiggle perf heap        # take a heap snapshot# … use the screen you suspect a few times …squiggle perf heap        # take anothersquiggle perf heapdiff    # what grew between the last two, and what holds on to it

heapdiff lists what grew between the two snapshots, by type, and for the biggest one, what is keeping it in memory. The Resources section shows the same comparison for the session’s two newest snapshots, and its Take heap snapshot button takes a snapshot without the terminal.

Loading the live Studio…
Memory and network requests. Live, in your browser.Open in Studio

The heap-growth detector points you here: memory that grows on every visit to the same screen.

Crashes

A native crash leaves a report on your Mac, not in the app. Each time Squiggle connects to your app, it stores the new reports from your app in ~/Library/Logs/DiagnosticReports with that session. The crash ended the launch before it, so a crash shows up in the session after the one it happened in, with the time it happened in its message. perf crashes lists the reports since the last scan, then the crashes of the app on that simulator stored so far, up to the newest 20, each with the session it’s stored with. Your agent’s perf_crashes stores the new reports, returns the same stored crashes and marks the ones it just stored new. A report is stored once, even when several Squiggle sessions scan at the same time. In Studio, press Scan for native crashes on the Insights section; the crashes found are listed there. Studio and the CLI remember how far they have scanned on each simulator, together, so neither stores a report twice. Only reports from the simulator the session ran on count, so the same app crashing on another simulator, such as one another session drives, isn’t stored with yours; the next session on that simulator stores it. A report of a fault the app lived through, such as the simulator’s XPC_EXIT_REASON_FAULT report, isn’t a crash, and neither lists it.

Terminal
squiggle perf crashes                     # find this app's new crash reportssquiggle perf crashes --commit            # and don't list them again next timesquiggle perf symbolicate --write         # turn JS frames back into your source linessquiggle perf symbolicate --write --binary ./build/Checkout.app/Checkout

JS frames are resolved through the Metro server each stack came from, unless you name one with --metro, SQUIGGLE_METRO_URL or metroUrl. Studio’s Symbolicate JS stacks button in the Data section does the same. Native frames need your app’s binary or dSYM, passed with --binary; without it they are reported as unresolved rather than left looking resolved.

Crashes in a React web app

A web page leaves no crash report. When the window’s page crashes, Squiggle stores it as a renderer_crash error in the session it ended, as it happens. perf crashes on a web session lists those and every error the app did not catch, with no scan. perf symbolicate maps each frame to your source file, line and column through the source maps of the scripts the page ran: from the dev server, or for a production build from the folder the build wrote its .map files to, since a production server usually serves none. A frame with no map, such as a library’s, stays as it was.

Terminal
squiggle perf symbolicate --write                 # through the dev server's source mapssquiggle perf symbolicate --write --maps ./dist   # a production build: the folder its .map files are in

Studio can also save a recording of the seconds before a crash; see Incident capture.

Open a session in another tool

Terminal
squiggle perf export --trace ./checkout-trace.json

In Studio, Export trace in the Data section does the same for the session you are looking at. The file opens in Perfetto or chrome://tracing, with the JS thread, the network, the UI and React renders on their own tracks, and metrics as counters. For a React web app, each perf profile taken during the session also records what Chrome DevTools’ Performance panel records (tasks, layout, paint, animation frames), and the export puts the browser’s own tracks beside Squiggle’s on one timeline.

Match an animation’s spring

To copy a motion you like, or check that a refactor kept it, squiggle motion taps a control, records how a view moves after the tap, and prints the withSpring config that reproduces it. Give it the view’s testID and the testID to tap; with two, it taps them in turn, so a motion that goes there and back is recorded both ways.

Terminal
squiggle motion sheet --tap open-sheet                       # fit the spring the sheet opens withsquiggle motion sheet --tap open-sheet,close-sheet --json > sheet.jsonsquiggle motion sheet --tap open-sheet,close-sheet --match sheet.json   # after you change the config

The printed config sets mass: 1. Reanimated’s default mass is 4, so keep the mass when you paste it. A motion that is not a plain spring, such as one retargeted while it runs, is reported as NOT a plain spring from rest with no config. It needs a debug build of the app on an iOS simulator. Every flag is in the CLI reference.

Map every spring a flow runs

squiggle flow <flow> --motion watches every view while the flow runs, so you see your app’s whole motion system at once instead of one view at a time. It runs the flow twice from a fresh launch, fits each element’s motion on the first run and checks the fit against the second. It needs a debug build of your app on an iOS simulator.

Terminal
squiggle flow oklch-labs --motion          # every spring the flow runs, with its polessquiggle flow oklch-labs --motion --json   # the fits, for a script

Each element that moved is listed as one of:

  • a spring, with its damping ratio, how long it takes to settle, and its two poles. The poles are where the spring sits in the complex plane: further left settles faster, further from the axis bounces more. Springs that share a token land on the same point.
  • a jump: the value reached its new place within two frames. A style that switches with no animation reads this way, and so does a spring given a duration in seconds where Reanimated expects milliseconds.
  • not a plain spring, with how far off the best spring was: a timing curve, or a spring retargeted while it was running.

An element is matched between the two runs by its testID, or by the line in your source that renders it. A view with neither can’t be matched, and the summary counts how many moved that way.

The run exits 1 when a value jumps, or when views rendered by one line of your source run different springs. Once it has found ten springs, it also exits 1 for a spring that is like no other in the app while the rest share theirs. A finding counts only when both runs show it, so a press too short to catch on one run makes none. Copy a spring’s config with squiggle motion, above.

Check that a scroll is smooth

Ask your agent to record the scroll and check it with the motion tool, or run squiggle motion --video on any recording. It reports how far the content moved in each frame and lists every problem with its time and frame numbers: frames that stopped mid-scroll, dropped or repeated frames, uneven steps, jumps and changes of direction. The verdict is still, smooth or janky, with a smoothness score: the share of moving frames with no problem.

Anything that stays put while the content moves, such as the keyboard, the status bar or a sticky header, pulls the measurement toward zero. Give the area that scrolls, and the areas to leave out, in pixels of the recording. It needs ffmpeg.

A recording Squiggle made (squiggle flow --record, a flow your agent runs with recording on, or one it films with record_start { video: true }) also says what your app was busy with during each problem, such as a long JavaScript task or the components that rendered. A video made any other way can’t be lined up with your app’s events, and the result says so.

To check one view without a recording, your agent can give the motion tool the view’s testID and what starts the motion (a tap, or a scroll). It follows the view on the running app at every frame the screen draws, so a dropped frame is one the screen really dropped. It follows the view’s own position, so give it the thing that moves (a row, a sheet), not a list whose content scrolls inside it. The view can be one the tap or scroll brings in, such as a sheet that slides up: it is followed from the first frame it appears in, and the tool tells you if it never appears within the window. Each problem then says what your app was busy with in those frames, such as a long JavaScript task, garbage collection or the components that rendered, so you know where to look. It needs your app running with Squiggle attached.

Catch a scroll that got worse

To catch a regression in CI, judge the new recording against a baseline of the same motion: an earlier recording, or a report saved with --json. It exits 1 only when a measure got worse by more than run-to-run noise. Your agent passes against to the motion tool for the same comparison.

Terminal
squiggle motion --video .squiggle/videos/feed.mp4 --against .squiggle/videos/feed-main.mp4 --chart feed-speed.png

--chart (chart: true for your agent) adds a picture of speed over time with every bad frame marked in red, ready to put on a pull request. To fail a flow when a view stutters, use assert: { smooth } with jankFrames, stallMs or smoothness; see Assertions.

Every flag is in the CLI reference.

Count things in a running app

Some questions need a number rather than a profile: how many animations are running right now, or how many times a component rendered. squiggle probe asks the running app over Metro, and can reach the Reanimated UI runtime as well as the JS one. It reads the Metro that serves your app (the one metroUrl names, when you set it) unless you pass --port. See the CLI reference.

For a React web app, pass --udid web:http://localhost:5173: the probe reads the window’s page with no Metro. probe cpu reports the page’s main thread, since a window has no process of its own. A browser has no worklet runtime, so probe anims, probe cell and --target ui stop with E_PROBE_UNSUPPORTED. probe replay --udid web:http://localhost:5173 replays a flow in Studio on the tab, once Studio has it focused under Devices.

Watch for a condition inside the app

A probe is a set of rules the app checks for you while you use it, with no rebuild: fps < 45 while a list scrolls, or a component rendering five times in one tick. You need a development build on a simulator; a release build never runs probes.

Terminal
squiggle probe inputs                                   # what a rule can readsquiggle probe when 'fps < 45' 'scroll.moved == 1' --id janky-scrollsquiggle probe when 'render.watch0 >= 5' --component ProductCard --for 30

A rule is an input, a comparison (>=, >, <=, <, ==, !=) and a number. The probe fires when all of its rules hold, or any of them with --any. Squiggle prints a line each time the rules start or stop holding, with the inputs at that moment, and removes the probe when you press Ctrl-C or --for runs out. To count one component’s renders, name it with --component (up to eight) and read render.watch0, render.watch1 and so on, in the same order.

The app refuses a probe it cannot run, and says why: an input it does not have, a range wider than the input’s, more than 256 instructions in one probe or 1,024 across all of them, or more than eight probes at once. Each check costs about 0.6 µs per instruction on the simulator, so a probe of a few rules costs a few microseconds a tick.

Keep a probe as a detector

When a probe has caught what you were looking for, export it with the same rules and id. Squiggle writes a TypeScript function that checks the rules, the firings it recorded as a fixture, and a test that checks the function against every one of them:

Terminal
squiggle probe export 'fps < 45' 'scroll.moved == 1' --id janky-scroll --out src/detectors

Export refuses rules that do not reproduce what the probe sent, so the fixture always matches the function. The test is written for Bun; pass --runner vitest or --runner jest for yours. The firings come from the newest session that has any for that id; --session picks another.