How to profile pure Dart code
Need to profile a Dart script without Flutter? Use the --observe flag to connect to DevTools.
dart run --observe --pause-isolates-on-start <filename>.dartThis will:
- Start the Dart VM Observatory (a web-based debugger)
- Pause execution before your code runs
- Print a URL like
http://127.0.0.1:8181/
Open that URL in Chrome, then click “Open DevTools” to access:
- CPU Profiler — see which functions consume the most time
- Memory — track allocations and find leaks
- Timeline — visualise async operations and isolate activity
The --pause-isolates-on-start flag ensures you don’t miss profiling the startup phase. Without it, your code might finish before you connect.
For production profiling, also consider:
# Profile with AOT compilation (closer to real-world performance)dart compile exe <filename>.dart -o app./appThis won’t have the Observatory, but gives accurate timings for AOT-compiled code.