JavaScript, React Native and Performance at react-europe 2016

77
JAVASCRIPT, REACT NATIVE & Performance @tadeuzagallo

Transcript of JavaScript, React Native and Performance at react-europe 2016

Page 1: JavaScript, React Native and Performance at react-europe 2016

JAVASCRIPT, REACT NATIVE &Performance

@tadeuzagallo

Page 2: JavaScript, React Native and Performance at react-europe 2016

ABOUT ME

@tadeuzagallo

Page 3: JavaScript, React Native and Performance at react-europe 2016

React Native StartupOVERVIEW

@tadeuzagallo

Page 4: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 5: JavaScript, React Native and Performance at react-europe 2016

React Native'sGOALS

@tadeuzagallo

Page 6: JavaScript, React Native and Performance at react-europe 2016

Hybrid APPS

@tadeuzagallo

Page 7: JavaScript, React Native and Performance at react-europe 2016

Performance Goals▸ Reduce Memory Usage▸ Reduce Startup Overhead

@tadeuzagallo

Page 8: JavaScript, React Native and Performance at react-europe 2016

NativeOPTIMISATIONS

@tadeuzagallo

Page 9: JavaScript, React Native and Performance at react-europe 2016

MULTI-THREADEDInitialisation

@tadeuzagallo

Page 10: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 11: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 12: JavaScript, React Native and Performance at react-europe 2016

LAZYModule Initialisation

@tadeuzagallo

Page 13: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 14: JavaScript, React Native and Performance at react-europe 2016

SmarterBATCHING

@tadeuzagallo

Page 15: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 16: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 17: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 18: JavaScript, React Native and Performance at react-europe 2016

JavaScriptOPTIMISATIONS

@tadeuzagallo

Page 19: JavaScript, React Native and Performance at react-europe 2016

INLINE/LAZYRequires

@tadeuzagallo

Page 20: JavaScript, React Native and Performance at react-europe 2016

var alert = require('alert');

// ...

<TouchableHighlight onPress={() => alert('Touched')}> // ...</Touchable>

@tadeuzagallo

Page 21: JavaScript, React Native and Performance at react-europe 2016

// ...

<TouchableHighlight onPress={() => require('alert')('Touched')}> // ...</Touchable>

@tadeuzagallo

Page 22: JavaScript, React Native and Performance at react-europe 2016

DEAD CODE ELIMINATION

@tadeuzagallo

Page 23: JavaScript, React Native and Performance at react-europe 2016

if (__DEV__) { require('DebugOnlyModule');}

const Alert = Platform.OS == 'ios' ? require('AlertIOS') : require('AlertAndroid');

@tadeuzagallo

Page 24: JavaScript, React Native and Performance at react-europe 2016

// dev=false&minify=true

// platform=ios

const Alert = require('AlertIOS');

// platform=android

const Alert = require('AlertAndroid');

@tadeuzagallo

Page 25: JavaScript, React Native and Performance at react-europe 2016

AND MORE...Optimise polyfills, extract babel helpers, ...

@tadeuzagallo

Page 26: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 27: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 28: JavaScript, React Native and Performance at react-europe 2016

What next?

@tadeuzagallo

Page 29: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 30: JavaScript, React Native and Performance at react-europe 2016

We shape our codeIN TERMS OF THE VM

@tadeuzagallo

Page 31: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 32: JavaScript, React Native and Performance at react-europe 2016

VMARCHITECTURE

@tadeuzagallo

Page 33: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 34: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 35: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 36: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 37: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 38: JavaScript, React Native and Performance at react-europe 2016

But on iOS...

@tadeuzagallo

Page 39: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 40: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 41: JavaScript, React Native and Performance at react-europe 2016

TL;DRTHIRD-PARTY APPS CAN'T JIT ON iOS

@tadeuzagallo

Page 42: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 43: JavaScript, React Native and Performance at react-europe 2016

WHAT ABOUTAndroid?

@tadeuzagallo

Page 44: JavaScript, React Native and Performance at react-europe 2016

WE CAN JIT ON Android!

@tadeuzagallo

Page 45: JavaScript, React Native and Performance at react-europe 2016

...BUT IT'S SLOWER*...

@tadeuzagallo

Page 46: JavaScript, React Native and Performance at react-europe 2016

ANDROID SETUP

@tadeuzagallo

Page 47: JavaScript, React Native and Performance at react-europe 2016

Profile, profile and profile

@tadeuzagallo

Page 48: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 49: JavaScript, React Native and Performance at react-europe 2016

Parsing isEXPENSIVE

@tadeuzagallo

Page 50: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 51: JavaScript, React Native and Performance at react-europe 2016

var hello = "Hello, World!";

var HelloWorld = function() { return React.createElement('div', null, hello);};

ReactDOM.render( React.createElement(HelloWorld), document.body);

@tadeuzagallo

Page 52: JavaScript, React Native and Performance at react-europe 2016

/* */ var hello = "Hello, World!";/* *//* */ var HelloWorld = function() {/* */ return React.createElement('div', null, hello);/* */ };/* *//* */ ReactDOM.render(/* */ React.createElement(HelloWorld),/* */ document.body);

@tadeuzagallo

Page 53: JavaScript, React Native and Performance at react-europe 2016

/* ==> */ var hello = "Hello, World!";/* *//* */ var HelloWorld = function() {/* */ return React.createElement('div', null, hello);/* */ };/* *//* */ ReactDOM.render(/* */ React.createElement(HelloWorld),/* */ document.body);

@tadeuzagallo

Page 54: JavaScript, React Native and Performance at react-europe 2016

/* */ var hello = "Hello, World!";/* *//* ==> */ var HelloWorld = function() {/* */ return React.createElement('div', null, hello);/* */ };/* *//* */ ReactDOM.render(/* */ React.createElement(HelloWorld),/* */ document.body);

@tadeuzagallo

Page 55: JavaScript, React Native and Performance at react-europe 2016

/* */ var hello = "Hello, World!";/* *//* */ var HelloWorld = function() {/* ==> */ return React.createElement('div', null, hello);/* */ };/* *//* */ ReactDOM.render(/* */ React.createElement(HelloWorld),/* */ document.body);

@tadeuzagallo

Page 56: JavaScript, React Native and Performance at react-europe 2016

/* */ var hello = "Hello, World!";/* *//* */ var HelloWorld = function() {/* */ return React.createElement('div', null, hello);/* */ };/* *//* ==> */ ReactDOM.render(/* */ React.createElement(HelloWorld),/* */ document.body);

@tadeuzagallo

Page 57: JavaScript, React Native and Performance at react-europe 2016

/* */ var hello = "Hello, World!";/* *//* */ var HelloWorld = function() {/* ==> */ return React.createElement('div', null, hello);/* */ };/* *//* */ ReactDOM.render(/* */ React.createElement(HelloWorld),/* */ document.body);

@tadeuzagallo

Page 58: JavaScript, React Native and Performance at react-europe 2016

Pre-parseCACHE

@tadeuzagallo

Page 59: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 60: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 61: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 62: JavaScript, React Native and Performance at react-europe 2016

BytecodeCACHE

@tadeuzagallo

Page 63: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 64: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 65: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 66: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 67: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 68: JavaScript, React Native and Performance at react-europe 2016

@tadeuzagallo

Page 69: JavaScript, React Native and Performance at react-europe 2016

Random Access Bundle

@tadeuzagallo

Page 70: JavaScript, React Native and Performance at react-europe 2016

import React, { Component } from 'react';import { Text, AppRegistry } from 'react-native';

var HelloWorld = () => ( <Text>Hello, World!</Text>);

AppRegistry.registerComponent('SampleApp', () => HelloWorld);

@tadeuzagallo

Page 71: JavaScript, React Native and Performance at react-europe 2016

__d('ReactComponent', function (global, require, module, exports) { /* ... */ });__d('react', function (global, require, module, exports) { /* ... */ });__d('AppRegistry', function (global, require, module, exports) { /* ... */ });__d('Text', function (global, require, module, exports) { /* ... */ });__d('react-native', function (global, require, module, exports) { /* ... */ });__d('SampleApp.js', function (global, require, module, exports) { /* ... */ });

@tadeuzagallo

Page 72: JavaScript, React Native and Performance at react-europe 2016

__d(0, function (global, require, module, exports) { /* ... */ });__d(1, function (global, require, module, exports) { /* ... */ });__d(2, function (global, require, module, exports) { /* ... */ });__d(3, function (global, require, module, exports) { /* ... */ });__d(4, function (global, require, module, exports) { /* ... */ });__d(5, function (global, require, module, exports) { /* ... */ });

@tadeuzagallo

Page 73: JavaScript, React Native and Performance at react-europe 2016

{ 0: { offset: 139, size: 202 }, 1: { offset: 342, size: 255 }, 2: { offset: 598, size: 115 }, 3: { offset: 714, size: 107 }, 4: { offset: 827, size: 131 }, 5: { offset: 959, size: 382 }}/* global code */\0__d(0, function (global, require, module, exports) { /* ... */ });\0__d(1, function (global, require, module, exports) { /* ... */ });\0__d(2, function (global, require, module, exports) { /* ... */ });\0__d(3, function (global, require, module, exports) { /* ... */ });\0__d(4, function (global, require, module, exports) { /* ... */ });\0__d(5, function (global, require, module, exports) { /* ... */ });\0

@tadeuzagallo

Page 74: JavaScript, React Native and Performance at react-europe 2016

// JavaScriptfunction require(module) { if (cached[module]) { return cached[module]; }

if (!factories[module]) { nativeRequire(module); }

cached[module] = factories[module]();}

// Nativevoid nativeRequire(module) { evaluate(RandomAccessBundle + offsets[module]);}

@tadeuzagallo

Page 75: JavaScript, React Native and Performance at react-europe 2016

react-native bundle --entry-file index.ios.js ...

@tadeuzagallo

Page 76: JavaScript, React Native and Performance at react-europe 2016

react-native unbundle --entry-file index.ios.js ...

@tadeuzagallo

Page 77: JavaScript, React Native and Performance at react-europe 2016

Thank you!

@tadeuzagallo