javascript performance pa1erns
play

JavaScript Performance Pa1erns @stoyanstefanov QCon San - PowerPoint PPT Presentation

JavaScript Performance Pa1erns @stoyanstefanov QCon San Francisco, Nov 8, 2012 JavaScript Performance Pa1erns Importance of Performance h1p://bookofspeed.com


  1. JavaScript ¡Performance ¡Pa1erns ¡ @stoyanstefanov ¡ QCon ¡ San ¡Francisco, ¡Nov ¡8, ¡2012 ¡

  2. JavaScript ¡Performance ¡Pa1erns ¡

  3. Importance ¡of ¡Performance ¡ h1p://bookofspeed.com ¡ ¡

  4. Importance ¡of ¡JavaScript ¡Performance ¡ h1p://h1parchive.org ¡ ¡

  5. // ¡todo ¡ 1. Loading ¡JavaScript ¡ 2. RunJme ¡/ ¡UI ¡/ ¡DOM ¡ ¡+ ¡benchmarks ¡ ¡+ ¡shims ¡

  6. Loading ¡

  7. First ¡things ¡first ¡ • reduce ¡# ¡of ¡script ¡files ¡ • gzip, ¡shave ¡70% ¡off ¡ • minify, ¡extra ¡40-­‑50% ¡ • Expires ¡headers ¡ • CDN ¡ h1p://yslow.org ¡ PageSpeed ¡ ¡ h1p://webpagetest.org ¡ ¡

  8. <script ¡src="h1p://…"> ¡ NOPE! ¡

  9. SPOF ¡ • Single ¡point ¡of ¡failure ¡ • JS ¡blocks ¡ h1p://phpied.com/3po-­‑fail ¡ SPOF-­‑O-­‑MaJc: ¡ ¡h1ps://chrome.google.com/webstore/detail/plikhggdplemddobondkeogomgoodeg ¡ ¡

  10. Off ¡the ¡criJcal ¡path ¡

  11. Asynchronous ¡JS ¡ • <script ¡defer> ¡ • <script ¡async> ¡ • unJl ¡then… ¡

  12. Dynamic ¡script ¡node ¡ var ¡js ¡= ¡document.createElement('script'); ¡ js.src ¡= ¡'h1p://cdn.com/my.js'; ¡ document.getElementsByTagName('head')[0].appendChild(js); ¡ h1p://calendar.perfplanet.com/2011/ the-­‑art-­‑and-­‑crao-­‑of-­‑the-­‑async-­‑snippet/ ¡

  13. But…, ¡bu1…, ¡bu1on? ¡ Q: ¡ <button ¡onclick="…" ? ¡ A: ¡To ¡hell ¡with ¡it ¡ ¡ Q: ¡Dependencies? ¡ A: ¡ onload ¡event ¡and ¡ js.onreadystatechange ¡ ¡ load('jquery.js', ¡'mystuff.js', ¡function ¡() ¡{ ¡ ¡ ¡mystuff.go(); ¡ }); ¡

  14. Unblocking ¡ onload ¡ • Async ¡JS ¡blocks ¡ window.onload ¡in ¡!IE ¡ • May ¡or ¡may ¡not ¡be ¡a ¡problem ¡ • There's ¡a ¡soluJon: ¡FIF ¡

  15. <fif> ¡ frame-­‑in-­‑frame ¡aka ¡friendly ¡frames ¡ aka ¡this ¡Meebo ¡thing ¡

  16. FIF ¡ 1) create ¡ iframe ¡src="js:false" ¡ 2) in ¡the ¡frame ¡ doc.write ¡a ¡ ¡ <body ¡onload ¡… ¡ 3) …that ¡loads ¡JS ¡

  17. FIF ¡(snippet) ¡ var ¡iframe ¡= ¡document.createElement('iframe'); ¡ document.body.appendChild(iframe); ¡ var ¡doc ¡= ¡iframe.contentWindow.document; ¡ doc.open().write('<body ¡onload="'+ ¡ ¡ ¡'var ¡js ¡= ¡document.createElement(\'script\');'+ ¡ ¡ ¡'js.src ¡= ¡\'h1p://example.org/js.js\';'+ ¡ ¡ ¡'document.body.appendChild(js);">'); ¡ doc.close(); ¡

  18. FIF ¡ • unblocks ¡ onload , ¡but… ¡ • more ¡complex ¡ • requires ¡JS ¡changes ¡

  19. your ¡script ¡(before) ¡ ¡ ¡ ¡ ¡// ¡fun ¡with ¡window ¡ ¡ ¡// ¡and ¡document ¡ ¡ ¡

  20. your ¡script ¡(before) ¡ (function() ¡{ ¡ ¡ ¡ ¡// ¡fun ¡with ¡window ¡ ¡ ¡// ¡and ¡document ¡ }()); ¡ ¡

  21. FIF ¡(aoer) ¡ (function(window) ¡{ ¡ ¡ ¡var ¡document ¡= ¡window.document; ¡ ¡ ¡// ¡fun ¡with ¡window ¡ ¡ ¡// ¡and ¡document ¡ }(parent.window)); ¡

  22. FIF ¡in ¡the ¡wild ¡ • experimental ¡support ¡in ¡FB ¡JS ¡SDK ¡ • h1p://jsbin.com/axibow/10/edit ¡ ¡ ¡ ¡

  23. </fif> ¡

  24. Load ¡JS ¡but ¡not ¡execute ¡ • Use ¡cases: ¡ – preload ¡in ¡anJcipaJon ¡ – lazy ¡

  25. Preload, ¡then ¡eventually ¡execute ¡ 1. fetch ¡the ¡script, ¡but ¡don’t ¡run ¡it ¡ 2. run ¡it ¡at ¡some ¡point ¡(same ¡as ¡async ¡JS) ¡

  26. Fetching ¡ • IE: ¡dynamic ¡script ¡node, ¡not ¡in ¡the ¡DOM ¡ • All ¡others: ¡CORS ¡(XHR2) ¡ – your ¡CDN ¡should ¡let ¡you ¡specify ¡ Access-­‑Control-­‑Allow-­‑Origin ¡ ¡ header ¡or ¡else! ¡

  27. Preload, ¡then ¡execute ¡ // preload var js = document.createElement('script'); if (!js.readyState || js.readyState !== 'uninitialized') { // non IE var xhr = new XMLHttpRequest(); if ('withCredentials' in xhr) { // XHR2 xhr.open('GET', url, false); xhr.send(null); } } js.src = url; // IE preloads! Thanks @getify // execute document.getElementsByTagName('head')[0].appendChild(js);

  28. // ¡todo ¡ 1. Loading ¡JavaScript ¡ 2. RunJme ¡/ ¡UI ¡/ ¡DOM ¡ ¡+ ¡benchmarks ¡ ¡+ ¡shims ¡

  29. Benchmarks ¡ • Lies, ¡damn ¡lies ¡and ¡performance ¡advice ¡ • Test ¡the ¡wrong ¡thing ¡ • Measure ¡the ¡wrong ¡thing ¡ • Even ¡if ¡not, ¡sJll ¡draw ¡the ¡wrong ¡conclusions ¡

  30. Your ¡first ¡benchmark ¡ var ¡start ¡= ¡new ¡Date(); ¡ // ¡loop ¡100000 ¡times ¡ var ¡took ¡= ¡new ¡Date() ¡– ¡start; ¡ NOPE! ¡

  31. Benchmark.js ¡ • by ¡John-­‑David ¡Dalton ¡ • used ¡in ¡h1p://jsperf.com ¡ ¡ – calibraJng ¡the ¡test ¡ – end ¡Jme ¡(ops/second) ¡ – staJsJcal ¡significance ¡ – margin ¡of ¡error ¡

  32. h1p://calendar.perfplanet.com/2010/ bulletproof-­‑javascript-­‑benchmarks/ ¡ ¡

  33. Benchmarking ¡browsers? ¡ No, ¡thanks ¡

  34. Let's ¡test! ¡

  35. String ¡concat? ¡ var ¡text ¡= ¡""; ¡ text ¡+= ¡"moar"; ¡ ¡ vs. ¡ ¡ var ¡parts ¡= ¡[]; ¡ parts.push('moar'); ¡ var ¡text ¡= ¡push.join(''); ¡ http://jsperf.com/join-concat/

  36. String ¡concat ¡

  37. The ¡pen ¡is ¡mighJer ¡than ¡the ¡sword! ¡* ¡ * ¡Only ¡if ¡the ¡sword ¡is ¡very ¡small ¡and ¡the ¡pen ¡very ¡sharp ¡

  38. "Don't ¡A, ¡B ¡is ¡so ¡much ¡faster!" ¡ You ¡should ¡check ¡it ¡again ¡

  39. Profiling ¡

  40. Picking ¡ba1les ¡

  41. DOM ¡

  42. DOM ¡ • DOM ¡is ¡slow ¡ • How ¡slow? ¡ ¡ • h1p://jsperf.com/dom-­‑touch ¡ ¡

  43. DOM ¡ // ¡DOM ¡ div.innerHTML ¡ ¡= ¡'a'; ¡ div.innerHTML ¡+= ¡'b'; ¡ ¡ // ¡string ¡ var ¡html ¡= ¡''; ¡ html ¡+= ¡'a'; ¡ html ¡+= ¡'b'; ¡ div.innerHTML ¡= ¡html; ¡

  44. DOM ¡

  45. DOM ¡+ ¡string ¡concat ¡ • put ¡things ¡in ¡perspecJve ¡ h1p://jsperf.com/dom-­‑touch-­‑concat ¡ ¡

  46. ECMAland ¡ DOMland ¡

  47. DOM ¡ • caching ¡DOM ¡references ¡ • caching ¡ length ¡in ¡collecJon ¡loops ¡ • "offline" ¡changes ¡in ¡document ¡fragment ¡ • batch ¡style ¡changes ¡ • reducing ¡reflows ¡and ¡repaints ¡

  48. reflows ¡ ¡ getComputedStyle() , ¡or ¡ currentStyle ¡in ¡IE ¡ bodystyle.color ¡= ¡'red'; ¡ tmp ¡= ¡computed.backgroundColor; ¡ bodystyle.color ¡= ¡'white'; ¡ tmp ¡= ¡computed.backgroundImage; ¡ bodystyle.color ¡= ¡'green'; ¡ tmp ¡= ¡computed.backgroundAttachment; ¡ ¡ bodystyle.color ¡= ¡'red'; ¡ bodystyle.color ¡= ¡'white'; ¡ bodystyle.color ¡= ¡'green'; ¡ tmp ¡= ¡computed.backgroundColor; ¡ tmp ¡= ¡computed.backgroundImage; ¡ tmp ¡= ¡computed.backgroundAttachment; ¡ ¡

  49. querySelectorSlow()? ¡ <table ¡border="1" ¡id="test-­‑table"> ¡ ¡ ¡<thead> ¡ ¡ ¡ ¡<!-­‑-­‑ ¡... ¡-­‑-­‑> ¡ ¡ ¡</thead> ¡ ¡ ¡<tbody> ¡ ¡ ¡ ¡ ¡<tr ¡class="rowme"> ¡ ¡ ¡ ¡ ¡ ¡ ¡<td>1</td><td>John</td><td><!-­‑-­‑ ¡... ¡-­‑-­‑> ¡ ¡ ¡ ¡ ¡ ¡ ¡<!-­‑-­‑ ¡... ¡-­‑-­‑> ¡

  50. querySelectorSlow()? ¡ var ¡trs ¡= ¡ ¡ ¡ ¡tbody.getElementsByClassName('rowme'); ¡ ¡ var ¡trs ¡= ¡ ¡ ¡ ¡tbody.getElementsByTagName('tr'); ¡ ¡ var ¡trs ¡= ¡ ¡ ¡ ¡tbody.querySelectorAll('.rowme'); ¡

  51. http://jsperf.com/queryinging/4 ¡ ¡

  52. querySelectorSlow()? ¡ for ¡( ¡ ¡ ¡var ¡i ¡= ¡0, ¡len ¡= ¡trs.length; ¡ ¡ ¡i ¡< ¡len; ¡ ¡ ¡i ¡+= ¡2) ¡{ ¡ ¡ ¡ ¡trs[i].className; ¡ ¡ } ¡

  53. http://jsperf.com/queryinging/3 ¡ ¡ ¡

  54. querySelectorSlow()? ¡ for ¡( ¡ ¡ ¡var ¡i ¡= ¡0, ¡len ¡= ¡trs.length; ¡ ¡ ¡i ¡< ¡len; ¡ ¡ ¡i ¡+= ¡2) ¡{ ¡ ¡ ¡ ¡trs[i].className ¡= ¡"rowme ¡hilite"; ¡ ¡ } ¡

  55. http://jsperf.com/queryinging/2 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend