This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path05.js.optimization.html
executable file
·462 lines (413 loc) · 15.9 KB
/
05.js.optimization.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web & Mobile Development — 05.JS.Optimization</title>
<meta name="description" content="Web & Mobile Development — 05.JS.Optimization">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- <link rel="stylesheet" href="assets/05/examples.css"> -->
<link rel="stylesheet" href="lib/zenburn.css">
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Web & Mobile Development <small>JLW288</small></h3>
<h1>05.JS.Optimization</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if(navigator.userAgent.match(/(iPhone|iPad|iPod|Android)/i))
document.write('<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>');
</script>
</section>
</section>
<!-- 1 : Intro -->
<section>
<section>
<h2>Optimization</h2>
<p><img src="assets/05/futurama_zapp.jpg" alt="" title="" height="309" width="446" /></p>
<q>If we hit that bullseye, the rest of the dominoes should fall like a house of cards. Checkmate.</q>
</section>
<section>
<h2>Optimization</h2>
<ul>
<li class="fragment">
A collection of tips and tricks to writing better JS
</li>
<li class="fragment">
A collection of tips and tricks to making better websites; speedwise and sizewise.
</li>
</ul>
<footer class="fragment">Note: the types of tips are intermingled on purpose, because they're all equally important</footer>
</section>
<section>
<h2>Put scripts at the bottom</h2>
<blockquote>The problem caused by scripts is that they block parallel downloads. The <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.1.4">HTTP/1.1 specification</a> suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.</blockquote>
<p>Including External Scripts <small><a href="01.advanced.js.html#/8/38">⚑</a></small></p>
</section>
<section>
<h2>Keep HTTP requests to a minimum</h2>
<ul>
<li class="fragment">
<a href="https://developers.google.com/speed/docs/best-practices/caching">Optimize caching</a> by using <code>Expires</code> and <code>Cache-Control: max-age</code> headerfs
</li>
<li class="fragment">
Collate CSS/JS
<ul>
<li class="fragment">Combine scripts to get fewer files <em>(and thus less requests!)</em></li>
</ul>
</li>
<li class="fragment">base64 encode images where possible
<ul>
<li>viz. icons used in CSS</li>
</ul>
</li>
<li class="fragment">
Load libraries from a Content Delivery Network
<ul>
<li class="fragment"><a href="http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery">CDN Hosted jQuery</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Make it understandable</h2>
<ul>
<li class="fragment">
Give your variables a short and easy to understand name
<ul>
<li class="fragment">Good: <code>age</code>, <code>firstName</code></li>
<li class="fragment">Bad: <code>createNewMemberIfAgeOverEighteenAndMoonIsFull</code></li>
<li class="fragment">Exceptions: counters in loops such <code>x</code> and <code>y</code></li>
</ul>
</li>
<li class="fragment">
Don't limit yourself in naming your variables
<ul>
<li class="fragment">Better to use <code>isLegalAge(18, age)</code> than <code>isOverEighteen()</code> as it gives your more <em>wiggle room</em></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>No global loitering</h2>
<ul>
<li class="fragment">Always use the <code>var</code> keyword when declaring variables <small><a href="01.advanced.js.html#/3/23">⚑</a></small></li>
<li class="fragment">Use namespaces (Object Literal)<small><a href="01.advanced.js.html#/8/43">⚑</a></small></li>
<li class="fragment">Use (revealing) module pattern <small><a href="01.advanced.js.html#/7/13">⚑</a></small></li>
</ul>
</section>
<section>
<h2>Annotate your code</h2>
<ul>
<li class="fragment">
Clean “good” code is good but not enough.
</li>
<li class="fragment">
Code needs comments
<ul>
<li class="fragment">The right kind of comments</li>
</ul>
</li>
<li class="fragment">
Comment the <em>why</em>, not the <em>what</em>
<ul>
<li class="fragment">The code is the <em>what</em>.</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Don't overmanipulate CSS Properties</h2>
<ul>
<li class="fragment">
Keep style definitions in your CSS
<ul>
<li class="fragment">Why add a <code>border</code> of <code>1px solid red</code> and change the <code>color</code> to <code>red</code> and make the text <code>bold</code> and set the <code>background</code> to something <code>red</code>-ish if you can just add the class <code>.error</code> to an element?</li>
</ul>
</li>
<li class="fragment">Easier to change when needed</li>
</ul>
</section>
<section>
<h2>Optimize scripts download speed</h2>
<ul>
<li class="fragment">
Use shorthands where possible
<ul>
<li class="fragment">literals, <code>0px</code> → <code>0</code>, <code>#FFFFFF</code> → <code>#FFF</code>, etc.</li>
<li class="fragment">A character saved is a character less to download</li>
</ul>
</li>
<li class="fragment">
Minify JS/CSS
<ul>
<li class="fragment">Strip all whitespace, comments, etc. to get smaller files</li>
</ul>
</li>
<li class="fragment">
Use <a href="http://en.wikipedia.org/wiki/HTTP_compression">HTTP compression</a> (Gzip)
</li>
<li class="fragment">
Compress images using <a href="http://tinypng.org/">tinypng</a>, <a href="http://www.smushit.com/">smushit</a>, <a href="http://imageoptim.com/">ImageOptim</a>, etc.
</li>
<li class="fragment">Add a few extra DNS <code>CNAME</code> records to your domain to have your server serve more parallel requests</li>
</ul>
</section>
<section>
<h2>Allow for configuration</h2>
<ul>
<li class="fragment">
Users of your scripts will want to customize the output
<ul>
<li class="fragment">Very easy with jQuery's <code>.extend</code> <small><a href="03.jquery.intro.html#/8/2">⚑</a></small></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Keep DOM lookups to a minimum</h2>
<ul>
<li class="fragment">
Optimize Loops <small><a href="01.advanced.js.html#/4/3">⚑</a></small>
</li>
<li class="fragment">
Cache DOM lookups <small><a href="01.advanced.js.html#/8/8">⚑</a></small> and/or make use of chaining <small><a href="01.advanced.js.html#/5/4">⚑</a></small>
</li>
</ul>
</section>
<section>
<h2>Keep DOM Manipulations to a minimum</h2>
<ul>
<li class="fragment">
Bad
<pre class="bigger"><code class="language-javascript dontrun">var myList = $('#myList');
for (var i = 0; i < 1000; i++){
myList.append('<li>This is list item ' + i + '</li>');
}</code></pre>
</li>
<li class="fragment">
Good
<pre class="bigger"><code class="language-javascript dontrun">var myList = $('#myList');
var myListItems = '';
for (var i = 0; i < 1000; i++) {
myListItems += '<li>This is list item ' + i + '</li>';
}
myList.html(myListItems);</code></pre>
</li>
</ul>
</section>
<section>
<h2>Prevent excessive redraws</h2>
<ul>
<li class="fragment">
Bad
<pre class="bigger"><code class="language-javascript dontrun">var items = $('li');
items.css('background-color','red').css('color','lime').css('border','1px solid blue');</code></pre>
</li>
<li class="fragment">
Good
<pre class="bigger"><code class="language-javascript dontrun">var items = $('li');
items.css({
'background-color': 'red',
'color': 'lime',
'border': '1px solid blue'
});</code></pre>
</li>
</ul>
</section>
<section>
<h2>Don't trust user data</h2>
<ul>
<li class="fragment">
Can easily be manipulated with Firebug
</li>
<li class="fragment">
Cast vars where possible
<ul>
<li class="fragment">Always provide a <code>radix</code> when using <code>parseInt</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>When using jQuery, always use the latest version</h2>
<ul>
<li class="fragment">
New versions not only contain new features but also
<ul>
<li class="fragment">Improvements</li>
<li class="fragment">Bug Fixes</li>
</ul>
</li>
<li class="fragment">
jQuery Version Speed Comparison: <a href="http://jsperf.com/jquery-1-4-2-vs-1-6-2-comparisons/5">jsPerf Test</a>
</li>
</ul>
</section>
<section>
<h2>Make good use of bubbling</h2>
<ul>
<li class="fragment">
Use a delegate: Don't bind event handlers on 1000 table cells, but bind 1 event handler on the table and let the event bubble up
</li>
<li class="fragment">In jQuery
<ul>
<li class="fragment">
Use <code>$.fn.on()</code> with extra selector string to filter the descendants of the selected elements that trigger the event
<pre class="bigger"><code class="language-javascript dontrun">$('table').on('click', 'td', function(e) { ... });</code></pre>
</li>
<li class="fragment">
Never use <code>$.fn.live()</code>
<ul>
<li><a href="http://jupiterjs.com/news/why-you-should-never-use-jquery-live">Never, ever</a></li>
</ul>
</li>
<li class="fragment">Use of <code>$.fn.delegate()</code> is possible, but we prefer the variant of <code>$.fn.on()</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Know your selectors</h2>
<ul>
<li>
The need for speed
<ul>
<li class="fragment">
Fast: <code>#id</code>, <code>element</code>
<ul>
<li>Can be passed directly into native versions</li>
</ul>
</li>
<li class="fragment">
Slower: <code>.class</code>
<ul>
<li>No <code>Document#getElementsByClassName</code> in IE5-8!</li>
</ul>
</li>
<li class="fragment">
Slowest: pseudo (viz. <code>:visible</code>) & attribute (<code>[title="foo"]</code>)
<ul>
<li>Only modern browsers will benefit</li>
<li>In jQuery: Try to split those off into sub-selections with <code>.find()</code></li>
</ul>
</li>
</ul>
</li>
<li class="fragment">
Don't overcomplicate selectors
</li>
<li class="fragment">
Be specific at the end (engines work right-to-left)
</li>
<li class="fragment">
Not all selectors are created equal: <a href="http://jsperf.com/jquery-selectors-context/2">jsPerf Test</a>
<ul>
<li>Use context param or use <code>.find()</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Make use of modern code</h2>
<ul>
<li class="fragment">
Use <code>requestAnimationFrame()</code> instead of <code>setTimeout()</code>
<ul>
<li class="fragment">Hardware acceleration!</li>
<li class="fragment">Aware if a tab is active or not</li>
</ul>
</li>
<li class="fragment">Use CSS3 animations instead of changing CSS properties via JS (<a href="http://paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/">video</a>)
<ul>
<li class="fragment">Again: Hardware acceleration!</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Go Plain Vanilla</h2>
<ul>
<li class="fragment">
“Bad”
<pre class="bigger"><code class="language-javascript dontrun">$('a').on('click', function(e) {
alert($(this).attr('href'));
});</code></pre>
</li>
<li class="fragment">
Better
<pre class="bigger"><code class="language-javascript dontrun">$('a').on('click', function(e) {
alert(this.href);
});</code></pre>
</li>
<li class="fragment">
Best
<pre class="bigger"><code class="language-javascript dontrun">document.querySelectorAll('a').addEventListener('click', function(e) {
alert(this.href);
});</code></pre>
</li>
<li class="fragment">
In modern browsers: <a href="http://sharedfil.es/js-48hIfQE4XK.html">Vanilla JS FTW</a>
</li>
</ul>
</section>
</section>
<!-- The END -->
<section>
<section>
<h2>Questions?</h2>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
</section>
</section>
<!-- Sources -->
<section id="sources">
<section>
<h2>Sources</h2>
<ul>
<li><a href="http://developer.yahoo.com/performance/rules.html#js_bottom">Put scripts at the bottom</a></li>
<li><a href="http://www.slideshare.net/cheilmann/javascript-best-practices-1041724">JavaScript Best Practices</a></li>
<li><a href="http://addyosmani.com/jqprovenperformance/">jQuery Proven Performance</a></li>
<li><a href="http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx">Improve your jQuery — 25 Excellent Tips</a></li>
</ul>
</section>
</section>
</div>
<!-- The navigational controls UI -->
<aside class="controls">
<a class="left" href="#">◄</a>
<a class="right" href="#">►</a>
<a class="up" href="#">▲</a>
<a class="down" href="#">▼</a>
<span id="revealIndex">/</span>
</aside>
<!-- Index Link -->
<aside class="back">
<a href="index.html">← Back to index</a>
</aside>
<!-- ikdoeict.be Link -->
<a href="http://www.ikdoeict.be/" title="ikdoeict.be" id="ikdoeict">ikdoeict.be</a>
<!-- Displays presentation progress, max value changes via JS to reflect # of slides -->
<div class="progress"><span></span></div>
</div>
<script src="js/reveal.js"></script>
<script src="lib/highlight.js"></script>
<script src="lib/prefixfree.js"></script>
<script src="lib/css-snippets.js"></script>
<script src="lib/css-edit.js"></script>
<script src="lib/incrementable.js"></script>
<script src="js/main.js"></script>
</body>
</html>