Language 9 of 10 · Topic 0.8

Dart Concurrency: Isolates, Event Loop & compute()

1Concept

Dart runs in a single-threaded Event Loop inside an Isolate with its own private heap memory. To run heavy computations without dropping UI frame rates (120 FPS), Dart spawns background Isolates (compute / Isolate.spawn) communicating via SendPort/ReceivePort.

2Architecture Diagram

[ Main UI Isolate (120 FPS) ] ──(SendPort Message)──► [ Background Worker Isolate ]
   (No Shared Memory Heap!)                              (Processes heavy JSON/CPU)

3Code Example

Stage 0 Language Foundations
import 'dart:async';

Future<int> heavyTask() async {
  await Future.delayed(Duration(milliseconds: 50));
  return 42 * 100;
}

Future<void> main() async {
  print('Starting async task...');
  int result = await heavyTask();
  print('Result from task: $result');
}

4Expected Output

Starting async task...
Result from task: 4200

5Key Takeaways

  • Isolates do not share memory; they communicate by passing message copies.
  • Async/await handles I/O on the main isolate without blocking UI rendering.
  • Use compute() for heavy JSON parsing or image resizing.