Slide 1

Slide 1 text

State Of The Union DevTools jQuery UK, 2015 @addyosmani

Slide 2

Slide 2 text

Network Improvements DevTools: State Of The Union March, 2015. Agenda Performance Case Study Whirlwind tour of new features

Slide 3

Slide 3 text

DevTools: State Of The Union Performance Matters Challenges: Network, Paint, Layout, Recalc Styles Our users have better experiences when the web is fast. This is why speed is one of the major factors in ranking.

Slide 4

Slide 4 text

We think pages should be rendered in under one second and hit 60fps for animations and transitions. To help frame how you should think about performance this year, here’s a new model that’s useful to consider...

Slide 5

Slide 5 text

DevTools: State Of The Union Image credits: GestureWorks, Timothy Miller, Last Call Media Inc. 100ms User interacts with app UI causing state change. Response 2015 Web Performance Model 6ms Animations and transitions, either autonomous or user- initiated. Animation 50ms App UI is in a stable state, awaiting user interaction. Idle 1000ms Time from app start to appearing ready for use. Load

Slide 6

Slide 6 text

DevTools: State Of The Union Learn more in “Making a silky smooth web” by Paul Lewis time finger down finger up do idle/cleanup in 50ms chunks in case finger down happens again animation Max: 100ms loading 1000ms 60fps. max 6ms chunks Read more: http://bit.ly/blink-midnight-train 2015 Web Performance Budget

Slide 7

Slide 7 text

Today we’ll look at how the DevTools have evolved to better visualise slowness in your pages.

Slide 8

Slide 8 text

New in Network Panel 1. What’s new? Chrome 42+

Slide 9

Slide 9 text

WebPageTest vs DevTools in Chrome 41 Chrome 41 had only two times: receive time & “everything else”. WebPageTest (top) color coded timing information. So much better.

Slide 10

Slide 10 text

What if this could be improved?

Slide 11

Slide 11 text

Update! Load timing is now more visible. New: Waterfall bars now show where time was spent!

Slide 12

Slide 12 text

DevTools: State Of The Union Hover over bars for more detailed timing breakdown Load event fired DOMContentLoaded event fired

Slide 13

Slide 13 text

DevTools: State Of The Union Details pane now docks to bottom when in Dock-to-right mode.

Slide 14

Slide 14 text

DevTools: State Of The Union Right-click to open Network files in Sources

Slide 15

Slide 15 text

DevTools: State Of The Union See what initiated a request. Now supports iframes (Other)

Slide 16

Slide 16 text

DevTools: State Of The Union Network protocol column (visualise use of HTTP/2, SPDY etc)

Slide 17

Slide 17 text

DevTools: State Of The Union Filter by method: GET/POST

Slide 18

Slide 18 text

DevTools: State Of The Union Filter by domain:

Slide 19

Slide 19 text

DevTools: State Of The Union Filter by mime-type:

Slide 20

Slide 20 text

DevTools: State Of The Union Negative text filters! Exclude matches from results:

Slide 21

Slide 21 text

DevTools: State Of The Union Preserve Log - navigations can keep adding to current waterfall

Slide 22

Slide 22 text

DevTools: State Of The Union Tip: Toggle columns displayed in the Network Panel

Slide 23

Slide 23 text

DevTools: State Of The Union Replay XHR can now replay with credentials, cookies & all. XMLHttpRequest. withCredentials is a boolean that indicates whether or not cross- site Access-Control requests should be made using credentials such as cookies or authorization headers. The default is false.

Slide 24

Slide 24 text

DevTools: State Of The Union Emulate spotty network connections in Device Mode!

Slide 25

Slide 25 text

DevTools: State Of The Union Pro-tip: Click/hold Reload to empty cache & do a hard reload Normal Reload performs a standard refresh but tries to use the cache where possible. Hard Reload re-fetches everything. Empty Cache and Hard Reload completely empties local cache before re-fetching everything.

Slide 26

Slide 26 text

Performance Case Study 2. Wikipedia

Slide 27

Slide 27 text

Lately, the DevTools team have been diving in to performance audits of popular sites. Wikipedia was one of the first.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Specifically, they were looking at the performance of the Wikimedia visual editor. This one..

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

It turns out using jQuery for toggling visibility was their major performance bottleneck. This totally surprised them. Off the back of this, Paul shared a recommendation..

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

The advice was really saying, don’t use hide(), toggle(), : visible, or :hidden because under the hood they’ll call getComputedStyle() before setting any inline styles. This forces costly style recalculations. Before we continue, Paul wanted to say..

Slide 34

Slide 34 text

Tools Not Rules Paul’s sorry DevTools “Sorry everyone!”

Slide 35

Slide 35 text

He kinda broke one rule of performance profiling - Tools not Rules :) So, let’s try to justify his recommendations by digging into our tools.

Slide 36

Slide 36 text

Measurement time! Let’s start by enabling two new options - Causes & JS Profiler

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

JS Sampling JS Profiler integrates JavaScript Sampling in Timeline! Get charts of JS execution time, call stacks visualised next to paint & layout. This is a game changer.

Slide 39

Slide 39 text

The new Causes option can be enabled in the DevTools Timeline. Causes explains what’s happening in Timeline. Useful for seeing what caused layout invalidations, recalc styles & forced layout.

Slide 40

Slide 40 text

Timeline for Wikipedia Editor on first load took ~20 seconds!

Slide 41

Slide 41 text

Let’s break this into ~ 3 Acts of work.

Slide 42

Slide 42 text

Act 1

Slide 43

Slide 43 text

Wikipedia has an edit notice system. They wrote a jQuery plugin ($.getVisibleText) to determine if a notice was visually empty. They then filter out notices based on this check. Editor Notices Edit Notice System on Wikipedia.com

Slide 44

Slide 44 text

This causes some hefty recalc styles because the plugin relies on the :hidden selector behind the scenes...

Slide 45

Slide 45 text

Heavy ~120ms Recalc style

Slide 46

Slide 46 text

:hidden use in their jQuery plugin Jump to source from Timeline records Source for their $.getVisibleText plugin Pretty print optimised source so we can read it

Slide 47

Slide 47 text

Problem Too much magic in :hidden & the other visibility utilities. :hidden isn’t part of the CSS spec. In addition, jQuery can’t take advantage of perf boosts from using qSA().

Slide 48

Slide 48 text

Let’s see how Wikipedia are using their plugin in practice.

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

For jQuery’s :hidden, an element is assumed to be hidden if it or any of its parents consumes no space in the document. CSS visibility isn’t really taken into account. Notice the use of offsetWidth/offsetHeight. Reading these properties can cause costly layout (reflow) in the browser.

Slide 51

Slide 51 text

https://github.com/jquery/jquery/blob/master/src/css/hiddenVisibleSelectors.js

Slide 52

Slide 52 text

Advice Don’t trust :hidden. Use your own logic to compute visibility. element.hidden is just one option.

Slide 53

Slide 53 text

Let’s look at another really expensive block and the source of the parent function behind it..

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Looks straight-forward, right? We assume .children() requires getting childNodes and running matchesSelector() on each of them. But, there’s actually a lot more being done here. Let’s go back to our Timeline.

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

The blue parseHTML bits at the bottom of those flames is Sizzle’s code for handling qSA() regex. It’s weird that we’re hitting it every single time. Digging deeper, this is actually due to a bug in Sizzle where setDocument is called and expensive processing is performed each time jQuery is used on a document that isn’t window. document. The jQuery team are exploring this at the time of publishing.

Slide 59

Slide 59 text

Wikipedia ended up dropping their use of jQuery children():

Slide 60

Slide 60 text

Advice Be careful with .children(). Also just use querySelectorAll() & DOM methods where you can.

Slide 61

Slide 61 text

Another thing we noticed was the editor had a particularly gnarly DOM size. It was pretty huge across the board.

Slide 62

Slide 62 text

Large DOM Node Count DOM size is an issue here > 14,000 Elements! Crazy.

Slide 63

Slide 63 text

Advice Kill any superfluous spans/divs you don't absolutely need. When you have serious recalc style issues, try keeping your DOM element count under 1000 for more predictable performance.

Slide 64

Slide 64 text

Act 2

Slide 65

Slide 65 text

Next we’re going to jump into the JavaScript CPU profiler.

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

This one method oo.copy() is doing a LOT of JS work. They actually used jQuery.html() quite a lot behind the scenes. Wikipedia are working on eliminating all their .html() calls, but we can look at just one instance of them fixing this up..

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Advice Avoid $(elem).html(str) for DOM insertion, use innerHTML or DOM methods. They’re massively faster. Batch. Don’t stamp out HTML into DOM a ton of times. Just use innerHTML once.

Slide 71

Slide 71 text

Let’s look at our CPU profile some more.

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Crazy. createSurface() calls are taking a HUGE amount of time. 1/3rd of our editor initialisation is spent here. We’ve also got some pretty hefty recalc styles for the method, taking > 100ms.

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

If you look at the source, they’re using the .css() method to set visibility and remove hidden classes.

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

This leads us to our largest bottleneck in the entire editor...

Slide 78

Slide 78 text

Our largest bottleneck curCSS!

Slide 79

Slide 79 text

It’s got the heaviest self-time stack of JS during editor load, coming from one jQuery curCSS call of ~319ms. Almost all of this work is coming from .hide() and .show(). In fact, this is littered throughout our traces.

Slide 80

Slide 80 text

100ms recalc styles. Lots of $(elem).hide(), $(elem).css(prop)

Slide 81

Slide 81 text

If curCSS sounds familiar, it’s because you’ll see it when digging into the source for jQuery’s .css() method. If a computed value exists for an element, it’ll use curCSS to go and set your styles.

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Wikipedia were very surprised when they learned .hide() was their biggest bottleneck. Their usage of it was pretty atypical for jQuery code.

Slide 84

Slide 84 text

Their usage

Slide 85

Slide 85 text

Unfortunately, there’s a lot more magic going on here. jQuery is calling getComputedStyle() before setting any inline styles for visibility. That isn’t a good call & is a good way to cause layout. They do it to support two edge-cases: display:none & inline styles that you might want to use with .show()/.hide(). jQuery are exploring optimisations for these issues at the time of publishing.

Slide 86

Slide 86 text

Advice Don’t use .hide() for high performance visibility toggling. Use el.hidden = true/false or add/remove/toggle class.

Slide 87

Slide 87 text

Act 3

Slide 88

Slide 88 text

140ms recalcs on .show/.hide() DevTools: State Of The Union More visibility toggling slowness Lots more jQuery.html(str) 300ms recalcs in $.fn.animate()

Slide 89

Slide 89 text

Overall advice If you think it’s slow, profile it. I’m not saying don’t use jQuery. I’m saying understand how your abstractions work. Know where your bottlenecks are.

Slide 90

Slide 90 text

Summary jQuery is not your friend for high-performance visibility toggling Track visibility on your own rather than using :hidden, :visible, .show(), .hide() Avoid $(elem).html(str) for DOM insertion. Use innerHTML or DOM methods. jQuery does a lot of magic around querySelectorAll. Use it on its own if possible. Keep your DOM size low for predictable performance Try to touch the DOM less. Batch changes. Mutations just make things harder. DevTools: State Of The Union Wikipedia Case Study

Slide 91

Slide 91 text

DevTools: State Of The Union SFGate.com ..and It’s not just Wikipedia! For more case studies, keep an eye on Paul Irish’s blog!

Slide 92

Slide 92 text

Grab bag of tips 3. Chrome 42+

Slide 93

Slide 93 text

DevTools: State Of The Union New Eye Dropper in the Element panel Color Picker!

Slide 94

Slide 94 text

DevTools: State Of The Union New! We now highlight DOM updates. Visualise what parts of a node have changed with ease:

Slide 95

Slide 95 text

DevTools: State Of The Union New Cubic Bezier Function Editor! With preconfigured bezier functions!

Slide 96

Slide 96 text

DevTools: State Of The Union Timeline: Check “Paint” for graphics layer insights

Slide 97

Slide 97 text

DevTools: State Of The Union Timeline: New Layers panel for advanced profiling

Slide 98

Slide 98 text

DevTools: State Of The Union 3D view for visualising composited layer stacking

Slide 99

Slide 99 text

DevTools: State Of The Union Inspect individual layers with ease

Slide 100

Slide 100 text

DevTools: State Of The Union ● What Layers were created? ● What were the reasons for compositing? ● Why does an element have those dimensions? ● See memory estimates Layers are those portions of a page uploaded to the GPU for compositing. Layers: dive into reasons for compositing, memory, slowness.

Slide 101

Slide 101 text

DevTools: State Of The Union New Paint Profiler! See draw calls executed to draw pages

Slide 102

Slide 102 text

DevTools: State Of The Union New! Web Animation playback: Playback and speed controls for controlling animation. Slow motion. Pause.

Slide 103

Slide 103 text

DevTools: State Of The Union Animation Inspector (coming soon!) In Experiments at the time of publishing

Slide 104

Slide 104 text

DevTools: State Of The Union Console: Inspecting an object? Right-click functions to jump to their definition in your source code

Slide 105

Slide 105 text

DevTools: State Of The Union New editing features in the Sources Panel! Multiple cursor support Autocompletion Column selection Ctrl + M to jump between matching brackets

Slide 106

Slide 106 text

DevTools: State Of The Union V8 Proactive Compilation - spot errors in your source earlier

Slide 107

Slide 107 text

DevTools: State Of The Union Save Console Messages to a Text file

Slide 108

Slide 108 text

DevTools: State Of The Union New! Goodbye “undefined is not a function” Undefined method names now inform us which object they were called on. We’ve got callsite awareness! Rather than just “undefined” in this example we get foo.bar.

Slide 109

Slide 109 text

DevTools: State Of The Union Stepping through apps that use a Framework is hard. Usually end up stepping into the Framework :(

Slide 110

Slide 110 text

DevTools: State Of The Union JavaScript Framework Blackboxing fixes that. Right click on a Framework/Library script to blackbox..and..

Slide 111

Slide 111 text

DevTools: State Of The Union BOOM. When stepping in/out/over, DevTools now bypasses the library code! Just debug your own sources. ● Exceptions thrown from library code will not pause ● Stepping into/out/over bypasses the library code ● Event listener breakpoints don't break in library code ● The debugger will not pause on any breakpoints set in library code.

Slide 112

Slide 112 text

DevTools: State Of The Union Pre-configure frameworks to blackbox from Settings

Slide 113

Slide 113 text

DevTools: State Of The Union New Promises Inspector (coming soon!) In Experiments at the time of publishing

Slide 114

Slide 114 text

DevTools: State Of The Union Filter by Pending, Fulfilled, Rejected Promises with ease

Slide 115

Slide 115 text

DevTools: State Of The Union Click-to-jump to where a Promise was defined:

Slide 116

Slide 116 text

DevTools: State Of The Union Promises Inspector also supports Async call stacks! Enable Async call stack traces

Slide 117

Slide 117 text

We hope you’ve found this preview of what’s new in the DevTools useful. There’s a lot more to come!

Slide 118

Slide 118 text

devtools.chrome.com See our latest

Slide 119

Slide 119 text

See Making a silky smooth web by Paul Lewis at SmashingConf Want a deep dive on the 2015 Performance Model?

Slide 120

Slide 120 text

See DevTools for designers & talk to us about your ideas Designer?

Slide 121

Slide 121 text

console.thanks() +AddyOsmani @addyosmani