React-Native Performance
Be more likes the natives
9th March 2018
Native
The good
â—Ź Fast
â—Ź Responsive
â—Ź Smooth animations
â—Ź Adhere to guidelines by Google and Apple
â—Ź Latest innovations from their large in-house dev teams
â—Ź Works well with the device hardware
Native
The not so good
â—Ź Different SDK tools
â—Ź Different frameworks
â—Ź Tend to bring out new languages e.g. Swift, Kotlin
â—Ź Costly $$
â—‹ x2 teams with specific knowledge
â—‹ x2 design, development, testing
â—Ź Little or no code sharing
â—Ź Takes time to develop
Hybrid
â—Ź Phonegap. Html5
â—Ź Cordova.
â—Ź Intel. Html5
â—Ź Xamarin. C#
â—Ź etc..
Majority uses Html5 and the reason they struggled was performance.. This was a bad
user experience and made no use of any shiny new tech your phone might have.
Always tough to talk to the hardware properly
React-Native
â—Ź Cross platform
â—Ź Write once
â—Ź Single code base
â—Ź Underpinned by React to only update the components it needs to
â—Ź Re-use existing skills from web development
â—Ź Less time to develop
â—Ź Single entry point for testing
â—Ź Debugging using Chrome debugger
â—Ź Hot-reloading
â—‹ Native you need to spin up the emulators each time. Slow. Time consuming
.
RN - Component mapping to Native
RN - Architecture
NativeJS
JS Engine called
JSCore.
Single Thread
Obj-C, Swift. Java.
Single Thread +
Background threads
But wait…
Think about “the performance”
Where’s my 60fps
Performance
● We simply can’t call native functions in a synchronous manner.
â—‹ User would be left waiting and wastes time which we could use to do more useful stuff
Performance
â—Ź Asynchronous is out too.
○ It’s better, but having lots of asynchronous calls would all add up
â—‹ So what to do?
JS
Fast
Native
Fast
RN - Architecture
Native
Fast
JS
Fast
Slow
The Bridge
Another Bridge
Problems with the Bridge
● We’re dealing with 2 different realms
â—Ź Vars declared on one side cannot be accessed on the other side
â—Ź Traversing the bridge needs to be kept to a minimum
â—Ź JS is running on a single thread
● Native is running on a single thread… although may have background threads
● For each frame, we don’t want to be crossing the bridge
Bridge 2.0 - The RN Approach
â—Ź Their bridge is Asynchronous, Batched and Serializable
â—Ź Keeps traversing over the bridge to a minimum
â—Ź Let the JS side batch up what needs to happen and send over the Bridge to Native.
â—Ź It does this by batching events for transportation
â—‹ E.g. If there a list of animation events, these are batched and sent over the bridge for processing at
Native side. Native can handle it faster. You’re still doing Native - just less.
â—Ź A message queue is created, serialised and passed over the bridge.
â—Ź Holy grail is to declare what your intention is in JS and send once over to a native
driver
Bridge 2.0 - The RN Approach
â—Ź If you debug in Chrome you can see the JSON packets that are passed around
between layers using websockets. This is the serialised JSON
Caveats
â—Ź Writing the drivers is complex.
â—Ź Large part of RN is to provide suitable drivers to handle what comes across the
bridge
â—Ź If some functionality does not exist, you need to get your hands dirty and write
native drivers
Future
â—Ź Less bridge walking
â—Ź RN is constantly updating their API
â—Ź More heavy lifting on the Native side
â—Ź You can ask the native side to do a lot of the processing but this can be less
productive than doing it on the JS side.
â—‹ Sometimes unavoidable due to limitations of libs or functionality that is not yet supported.

Green Custard Friday Talk 5: React-Native Performance

  • 1.
    React-Native Performance Be morelikes the natives 9th March 2018
  • 2.
    Native The good â—Ź Fast â—ŹResponsive â—Ź Smooth animations â—Ź Adhere to guidelines by Google and Apple â—Ź Latest innovations from their large in-house dev teams â—Ź Works well with the device hardware
  • 3.
    Native The not sogood â—Ź Different SDK tools â—Ź Different frameworks â—Ź Tend to bring out new languages e.g. Swift, Kotlin â—Ź Costly $$ â—‹ x2 teams with specific knowledge â—‹ x2 design, development, testing â—Ź Little or no code sharing â—Ź Takes time to develop
  • 4.
    Hybrid â—Ź Phonegap. Html5 â—ŹCordova. â—Ź Intel. Html5 â—Ź Xamarin. C# â—Ź etc.. Majority uses Html5 and the reason they struggled was performance.. This was a bad user experience and made no use of any shiny new tech your phone might have. Always tough to talk to the hardware properly
  • 5.
    React-Native â—Ź Cross platform â—ŹWrite once â—Ź Single code base â—Ź Underpinned by React to only update the components it needs to â—Ź Re-use existing skills from web development â—Ź Less time to develop â—Ź Single entry point for testing â—Ź Debugging using Chrome debugger â—Ź Hot-reloading â—‹ Native you need to spin up the emulators each time. Slow. Time consuming .
  • 6.
    RN - Componentmapping to Native
  • 7.
    RN - Architecture NativeJS JSEngine called JSCore. Single Thread Obj-C, Swift. Java. Single Thread + Background threads
  • 8.
    But wait… Think about“the performance” Where’s my 60fps
  • 9.
    Performance ● We simplycan’t call native functions in a synchronous manner. ○ User would be left waiting and wastes time which we could use to do more useful stuff
  • 10.
    Performance ● Asynchronous isout too. ○ It’s better, but having lots of asynchronous calls would all add up ○ So what to do?
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    Problems with theBridge ● We’re dealing with 2 different realms ● Vars declared on one side cannot be accessed on the other side ● Traversing the bridge needs to be kept to a minimum ● JS is running on a single thread ● Native is running on a single thread… although may have background threads ● For each frame, we don’t want to be crossing the bridge
  • 16.
    Bridge 2.0 -The RN Approach ● Their bridge is Asynchronous, Batched and Serializable ● Keeps traversing over the bridge to a minimum ● Let the JS side batch up what needs to happen and send over the Bridge to Native. ● It does this by batching events for transportation ○ E.g. If there a list of animation events, these are batched and sent over the bridge for processing at Native side. Native can handle it faster. You’re still doing Native - just less. ● A message queue is created, serialised and passed over the bridge. ● Holy grail is to declare what your intention is in JS and send once over to a native driver
  • 17.
    Bridge 2.0 -The RN Approach â—Ź If you debug in Chrome you can see the JSON packets that are passed around between layers using websockets. This is the serialised JSON Caveats â—Ź Writing the drivers is complex. â—Ź Large part of RN is to provide suitable drivers to handle what comes across the bridge â—Ź If some functionality does not exist, you need to get your hands dirty and write native drivers
  • 19.
    Future â—Ź Less bridgewalking â—Ź RN is constantly updating their API â—Ź More heavy lifting on the Native side â—Ź You can ask the native side to do a lot of the processing but this can be less productive than doing it on the JS side. â—‹ Sometimes unavoidable due to limitations of libs or functionality that is not yet supported.