Top Banner
Production optimization with React and Webpack @k88hudson Firefox Engineering, Mozilla
72

Production optimization with React and Webpack

Apr 16, 2017

Download

Technology

k88hudson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Production optimization with React and Webpack

Production optimization withReact and Webpack

@k88hudson Firefox Engineering, Mozilla

🏎

Page 2: Production optimization with React and Webpack

Why React?

Page 3: Production optimization with React and Webpack

Abstracts away the DOM

Works on servers, native apps, …Firefox?!?

It’s fast*Helpful errors

Deterministic component rendering

Lifecycle

Testable

Page 4: Production optimization with React and Webpack

Why Webpack?

Page 5: Production optimization with React and Webpack
Page 6: Production optimization with React and Webpack

node_modules file system

single bundle for client-side applications

Page 7: Production optimization with React and Webpack

Why optimization?

Page 8: Production optimization with React and Webpack

ERROR! ERROR!

Page 9: Production optimization with React and Webpack

Users have high standards for performance

Page 10: Production optimization with React and Webpack

Good optimization is not just making everything faster, smaller and more

fault tolerant…

Page 11: Production optimization with React and Webpack

…it’s about being aware of your options and making the right

compromises.

Page 12: Production optimization with React and Webpack

Everyone has Opinions™

Page 13: Production optimization with React and Webpack

Laws of benevolent optimization

Page 14: Production optimization with React and Webpack

Laws of benevolent optimization

1. Don’t optimize prematurely 2. Know your options 3. Instrument your sh*t 4. Set benchmarks that make sense for

your actual users

Page 15: Production optimization with React and Webpack

1. Don’t optimize prematurely

Page 16: Production optimization with React and Webpack

Before you optimize…• Know what you’re building, address the UX/

product risk first

• Are your engineers on the same page as your designers/product? Do you share a common goal?

• Finish a few rounds of internal testing

• Have a good idea of who your users are

Page 17: Production optimization with React and Webpack

You might be ready to optimize if…• You’re about to ship a product to market,

particularly on devices or in conditions that are different from your development environment

• You’ve already shipped an MVP and are seeing latency/perf/errors bog you down

• You’ve already shipped a product and have no idea if it’s performing well but you feel like you should probably figure that out

Page 18: Production optimization with React and Webpack

2. Know your options

Page 19: Production optimization with React and Webpack

Where can we optimize?

Development Build time

RuntimeAnalysis

Page 20: Production optimization with React and Webpack

Webpack—optimizing at Build time

Development

Page 21: Production optimization with React and Webpack

Optimizing at build time

Transform code (for size, compatibility)

Manage configuration

Remove unused, non-essential code

Re-structure files/output

Page 22: Production optimization with React and Webpack

main.js

main.bundle.js

Page 23: Production optimization with React and Webpack

Typical Webpack set-up

Page 24: Production optimization with React and Webpack

Using custom loaders

Page 25: Production optimization with React and Webpack

Compiling ES2015 and JSX

<Foo bar />

React.createElement(Foo, { bar: true });

Page 26: Production optimization with React and Webpack

How big is the bundle?

Page 27: Production optimization with React and Webpack

Without webpack

react.js 628K react-dom.js 4.0k

Page 28: Production optimization with React and Webpack

With webpack

main.js 648K

Page 29: Production optimization with React and Webpack

Webpack’s built-in production optimization plugins

webpack -p

Page 30: Production optimization with React and Webpack

Webpack’s built-in production optimization plugins

• optimize-minimize (UglifyJS)

• optimize-occurrence-order

• optimize-dedupe

Page 31: Production optimization with React and Webpack

With webpack -p

main.js 172K (-476k)

Page 32: Production optimization with React and Webpack

Another option— Using the production build directly

Page 33: Production optimization with React and Webpack

With resolve/externals

main.js 136K (-512)

Page 34: Production optimization with React and Webpack

Use a production build of React

process.env.NODE_ENV

Page 35: Production optimization with React and Webpack

Use a production build of React

Page 36: Production optimization with React and Webpack

How DefinePlugin works

Page 37: Production optimization with React and Webpack

Using DefinePlugin for dev-only dependencies

my-dev-logger will not be included in the bundle.

Page 38: Production optimization with React and Webpack

With webpack -p NODE_ENV=production

main.js 128K (-520k)

Page 39: Production optimization with React and Webpack

This is stuff you should definitely be doing.

Page 40: Production optimization with React and Webpack

3. Instrument your sh*t

Page 41: Production optimization with React and Webpack

webpack --json

Page 42: Production optimization with React and Webpack
Page 43: Production optimization with React and Webpack

webpack-bundle-size-analyzer

Page 44: Production optimization with React and Webpack

React—optimizing at Runtime

Page 45: Production optimization with React and Webpack

First-load latency

Page 46: Production optimization with React and Webpack

First-load latency

Page 47: Production optimization with React and Webpack

First-load latency

Page 48: Production optimization with React and Webpack

General optimizations

Page 49: Production optimization with React and Webpack

If we render with JS…

Page 50: Production optimization with React and Webpack

loading…

Page 51: Production optimization with React and Webpack

Server-side rendering with react

Page 52: Production optimization with React and Webpack

Precompile the initial state

Page 53: Production optimization with React and Webpack

Instrumenting performance

Page 54: Production optimization with React and Webpack

Instrumenting performance

Page 55: Production optimization with React and Webpack

Time to first React render

Page 56: Production optimization with React and Webpack

Time to first React renderwith content

Page 57: Production optimization with React and Webpack

Total render time

Page 58: Production optimization with React and Webpack

What if we’re havingrendering issues?

Page 59: Production optimization with React and Webpack

Reasons this might be a problem

• You are manipulating or reading the DOM directly

• You are handling very large arrays of complex elements

• Your business logic contains computationally expensive functions

• You are rendering very complex DOM elements (such as SVG visualizations)

Page 60: Production optimization with React and Webpack

Manipulating/reading the DOM

Page 61: Production optimization with React and Webpack

Using keys correctly

Page 62: Production optimization with React and Webpack

Using keys correctly

Page 63: Production optimization with React and Webpack

Using keys correctly

Page 64: Production optimization with React and Webpack

shouldComponentUpdate

Page 65: Production optimization with React and Webpack

shouldComponentUpdate

Page 66: Production optimization with React and Webpack

Use React Perf toolsrequire(“react-addons-perf“);

Page 67: Production optimization with React and Webpack

Perf.printInclusive

Perf.printExclusive

Perf.printWasted

Perf.printOperations()

Page 68: Production optimization with React and Webpack

Laws of benevolent optimization

1. Don’t optimize prematurely 2. Know your options 3. Instrument your sh*t 4. Set benchmarks that make sense for

your actual users

Page 69: Production optimization with React and Webpack

What should you benchmark?

• File size

• Time to first load

• Time to render

Page 70: Production optimization with React and Webpack

What is acceptable depends on who your users are what they’re likely to expect

Page 71: Production optimization with React and Webpack

The difference between over-engineering and good engineering

is making the right compromises

Page 72: Production optimization with React and Webpack

Thanks! Tweet at me @k88hudson

😎