Klarna May 10, 2022

Tree Shaking React Native Apps

Article Summary

Klarna Engineering borrowed a web optimization technique and cut their React Native app startup time by 20%. Here's how tree shaking made the difference.

The Klarna team applied tree shaking (dead-code elimination) to their React Native app, tackling technical challenges around module systems and syntax compatibility. This detailed implementation guide shows how web bundling techniques can dramatically improve mobile performance.

Key Takeaways

Critical Insight

By implementing tree shaking in React Native, Klarna achieved a 20% median startup improvement and 46% smaller critical bundle, proving web optimization techniques translate to mobile.

The article reveals why React Native's mixed module system was the biggest obstacle and exactly how they worked around it.

About This Article

Problem

Klarna's React Native app had a module system problem. React Native mixed CommonJS and ES modules together, which broke Webpack's ability to detect exports and remove dead code.

Solution

The team fixed the syntax issues by turning on TypeScript's isolatedModules flag and adding type modifiers to imports. They also made sure that dependencies used modern ES module syntax so Hermes could process them without extra transpilation.

Impact

The changes increased build times to 4 minutes total, a 30% increase from before. However, the JavaScript bundle shrank by 14% overall across the entire application.