Question #63MediumFlutter Basics

Flutter What about the performance in flutter over native development ?

#flutter#performance#native

Answer

Overview

Flutter performance is near-native, achieving 95-98% of native performance in most scenarios. The key differences lie in rendering approach, compilation strategy, and platform integration.


Performance Comparison

MetricNative (Android/iOS)Flutter
UI Rendering60 FPS60 FPS
Startup time100% (baseline)95-98%
Frame renderingDirect platform callsSkia engine rendering
Animation performance60 FPS58-60 FPS
Heavy computations100%98%
App sizeSmaller (10-15 MB)Larger (15-20 MB + engine)
Memory usageLowerSlightly higher

Why Flutter is Fast

1. AOT Compilation (Release Mode)

Flutter compiles to native ARM code (Ahead-of-Time), not interpreted or JIT-compiled in production.

dart
// Dart code
void main() {
  runApp(MyApp());
}

Release build:

  • Compiled to native ARM assembly
  • No runtime interpretation
  • Direct machine code execution

2. Skia Rendering Engine

Flutter doesn't use platform widgets. It renders everything using Skia (Google's 2D graphics engine).

Advantages:

  • Consistent 60 FPS (120 FPS on capable devices)
  • Pixel-perfect UI across platforms
  • No platform widget overhead
dart
// Flutter draws every pixel
CustomPaint(
  painter: MyPainter(),
  child: Container(),
)

3. Widget Tree Optimization

Flutter's widget tree is optimized for minimal rebuilds.

dart
class MyWidget extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return Consumer<Model>(
      builder: (context, model, child) {
        return Text('${model.value}'); // Only rebuilds this
      },
    );
  }
}

Key optimizations:

  • Immutable widgets
  • Efficient diffing algorithm
  • Minimal rebuilds with
    text
    const
    constructors

Performance Benchmarks

Startup Time

text
Native Android:    300ms
Native iOS:        250ms
Flutter:           280-320ms (within 5-10% of native)

Scrolling Performance (60 FPS target)

text
Native:           60 FPS
Flutter:          60 FPS (simple lists)
                  58-60 FPS (complex lists with images)

Animation Performance

text
Native:           Smooth 60 FPS
Flutter:          Smooth 60 FPS (using AnimationController)
                  120 FPS on capable devices (iPad Pro, etc.)

Performance Optimizations in Flutter

1. Use const Constructors

dart
// ❌ Bad - Creates new widget every rebuild
Widget build(BuildContext context) {
  return Container(
    child: Text('Hello'),
  );
}

// ✅ Good - Reuses widget (no rebuild)
Widget build(BuildContext context) {
  return const Container(
    child: Text('Hello'),
  );
}

2. ListView.builder vs ListView

dart
// ❌ Bad - Creates all items at once
ListView(
  children: List.generate(1000, (i) => ListTile(title: Text('Item $i'))),
)

// ✅ Good - Lazy loading, creates only visible items
ListView.builder(
  itemCount: 1000,
  itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
)

3. Avoid Expensive Operations in build()

dart
// ❌ Bad - Expensive operation in build

Widget build(BuildContext context) {
  final data = expensiveComputation(); // Runs on every rebuild!
  return Text(data);
}

// ✅ Good - Cache in initState
String? _cachedData;


void initState() {
  super.initState();
  _cachedData = expensiveComputation(); // Runs once
}


Widget build(BuildContext context) {
  return Text(_cachedData!);
}

4. Use RepaintBoundary for Complex Animations

dart
RepaintBoundary(
  child: AnimatedWidget(), // Isolates repaints
)

5. Optimize Images

dart
// ✅ Use cached network images
CachedNetworkImage(
  imageUrl: 'https://example.com/image.jpg
  placeholder: (context, url) => CircularProgressIndicator(),
)

// ✅ Resize images
Image.network(
  'https://example.com/image.jpg
  width: 200, // Don't load full-size image
  height: 200,
)

Real-World Performance

Google Ads App (Flutter)

  • 60 FPS scrolling with complex animations
  • Instant screen transitions
  • Smooth interactions matching native

Alibaba (Flutter)

  • Handles millions of products
  • Fast search and filtering
  • Smooth animations and transitions

Where Native is Faster

1. Heavy Graphics (Games)

Native game engines (Unity, Unreal) outperform Flutter for:

  • 3D rendering
  • Complex physics simulations
  • High-performance gaming

Recommendation: Use native or game engines for complex games.


2. Video Processing

Native APIs for video encoding/decoding are more optimized.

Recommendation: Use platform channels for heavy video processing.


3. AR/VR Apps

ARKit (iOS) and ARCore (Android) have better performance natively.

Recommendation: Use native for AR/VR or use plugins like

text
ar_flutter_plugin
.


Performance Testing in Flutter

DevTools Performance Tab

bash
flutter run --profile
# Open DevTools → Performance tab

Metrics:

  • Frame rendering times
  • GPU/CPU usage
  • Memory consumption
  • Jank (dropped frames)

Performance Overlay

dart
MaterialApp(
  showPerformanceOverlay: true, // Shows FPS counter
  home: MyHomePage(),
)

Timeline Analysis

dart
import 'dart:developer';

void expensiveOperation() {
  Timeline.startSync('ExpensiveOperation');
  // Your code here
  Timeline.finishSync();
}

Memory Comparison

Native Android

text
App size:       10-15 MB
Memory usage:   40-60 MB (typical)

Flutter

text
App size:       15-20 MB (includes engine)
Memory usage:   50-70 MB (typical)

Difference: Flutter uses ~10-20% more memory due to engine overhead.


App Size Comparison

Native App

text
Android APK:    8-12 MB
iOS IPA:        10-15 MB

Flutter App

text
Android APK:    15-20 MB (includes Flutter engine ~4-5MB)
iOS IPA:        20-25 MB

Optimization:

  • Use code splitting
  • Enable tree shaking
  • Compress assets

Release Build Performance

Debug vs Profile vs Release

ModePerformanceUse Case
Debug10x slowerDevelopment (Hot Reload)
ProfileNear-releasePerformance testing
ReleaseMaximumProduction

Always test performance in Profile/Release mode!

bash
flutter run --release
flutter build apk --release

Performance Best Practices

PracticeImpactRecommendation
Use const widgetsHighAlways when possible
ListView.builderHighFor long lists
Avoid setState() in loopsHighUse single setState
Optimize imagesMediumResize, cache
Use RepaintBoundaryMediumFor complex animations
Profile before optimizingCriticalMeasure first!

Benchmark: Flutter vs Native

Scrolling 10,000 Items

text
Native Android:   60 FPS (0% jank)
Native iOS:       60 FPS (0% jank)
Flutter:          60 FPS (1-2% jank)

Complex Animation (Particle System)

text
Native:           60 FPS
Flutter:          58-60 FPS

Cold Startup Time

text
Native Android:   350ms
Native iOS:       280ms
Flutter:          320ms

Key Takeaways

Flutter performance: 95-98% of native in most scenarios

60 FPS rendering: Achieved in both native and Flutter

Startup time: Within 5-10% of native

Memory/Size: Slightly higher due to Flutter engine

Best for: UI-heavy apps, not heavy 3D games


When Performance Matters Most

Flutter is great for:

  • E-commerce apps
  • Social media
  • Productivity apps
  • Dashboard apps
  • Most mobile apps

Consider native for:

  • High-performance games (use Unity/Unreal)
  • AR/VR apps (use native ARKit/ARCore)
  • Heavy video processing
  • Apps requiring absolute maximum performance

Resources