Ajax Performance


Published on


Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ajax Performance

  1. 1. JavaScript: The Good Parts Part Six: Ajax Performance Douglas Crockford [email_address]
  2. 2. Memento
  3. 3. The Sessionless Web <ul><li>Cookies for pseudosessions. </li></ul><ul><li>Cookies enable CSRF attacks. </li></ul><ul><li>Every action results in a page replacement. </li></ul><ul><li>Pages are heavy, complicated, multipart things. </li></ul><ul><li>The web is a big step backwards in individual productivity. </li></ul>
  4. 4. “ When your only tool is a hammer, every problem looks like a webpage.”
  5. 5. The Ajax Revolution The page is an application with a data connection to a server.
  6. 6. When the user does something, we send a JSON message to the server, and receive a JSON message as the result.
  7. 7. A JSON message is less work for the server to generate, moves faster on the wire, and is less work for the browser to parse and render than an HTML document.
  8. 18. Division of Labor How is the application divided between the browser and the server?
  9. 19. Pendulum of Despair Server The browser is a terminal.
  10. 20. Pendulum of Despair Server Browser The browser is a terminal The server is a file system.
  11. 21. Seek the Middle Way. A pleasant dialogue between specialized peers.
  12. 22. Ajaxify <ul><li>The client and server are in a dialog. </li></ul><ul><li>Make the messages between them as small as possible. </li></ul><ul><li>The client does not need a copy of the database. It just needs, at any moment, just enough information to serve the user. </li></ul><ul><li>Don't rewrite the server application in JavaScript! </li></ul>
  13. 23. The Poor Browser <ul><li>The browser is a very inefficient application platform. </li></ul><ul><li>If your application becomes bloated, performance will be very bad. </li></ul><ul><li>Try to keep the client programming light. </li></ul>
  14. 24. Amazingly, the browser works <ul><li>But it doesn't work well. </li></ul><ul><li>It is a difficult platform to work with. </li></ul><ul><li>There are significant security problems. </li></ul><ul><li>There are significant performance problems. </li></ul><ul><li>It was not designed to be an application delivery system. </li></ul><ul><li>Ajax pushes the browser really hard. </li></ul>
  15. 25. Correctness First <ul><li>Do not worry about optimization until you have the application working correctly. </li></ul><ul><li>If it isn’t right, it doesn’t matter if it is fast. </li></ul><ul><li>Test for performance early. </li></ul><ul><li>Test in customer configurations: Slow networks, slow computers. </li></ul><ul><li>Internal networks and developer-class machines can mask performance problems. </li></ul>
  16. 26. Premature optimization is the root of all evil. — Donald Knuth <ul><li>Use YSlow to reduce startup time. </li></ul><ul><li>Don't optimize until you need to, but find out as early as possible if you need to. </li></ul><ul><li>Clean, correct code is easier to optimize. </li></ul><ul><li>Tweaking is usually ineffective. </li></ul><ul><li>Sometimes restructuring or redesign is required. </li></ul>
  17. 27. Example <ul><li>var fibonacci = function (n) { </li></ul><ul><li>return n < 2 ? n : </li></ul><ul><li>fibonacci(n - 1) + </li></ul><ul><li>fibonacci(n - 2); </li></ul><ul><li>}; </li></ul><ul><li>fibonacci(40) </li></ul><ul><li>Calls itself 331,160,280 times. </li></ul>
  18. 28. Memoizer <ul><li>var memoizer = function (memo, fundamental) { </li></ul><ul><li>var shell = function (n) { </li></ul><ul><li>var result = memo[n]; </li></ul><ul><li>if (typeof result !== 'number') { </li></ul><ul><li>result = fundamental(shell, n); </li></ul><ul><li>memo[n] = result; </li></ul><ul><li>} </li></ul><ul><li>return result; </li></ul><ul><li>}; </li></ul><ul><li>return shell; </li></ul><ul><li>}; </li></ul>
  19. 29. Memoizer <ul><li>var fibonacci = </li></ul><ul><li>memoizer([0, 1], function (recur, n) { </li></ul><ul><li>return recur(n - 1) + recur(n - 2); </li></ul><ul><li>}); </li></ul><ul><li>fibonacci(40) </li></ul><ul><li>Calls itself 38 times. </li></ul><ul><li>The key to optimization is work avoidance. </li></ul>
  20. 30. Code Quality <ul><li>High quality code is most likely to avoid platform problems. </li></ul><ul><li>Code Conventions for the JavaScript Programming Language </li></ul><ul><li>http://javascript.crockford.com/code.html </li></ul><ul><li>Use JSLint.com. Pass with no warnings. </li></ul>
  21. 31. Have regular code readings. <ul><li>Don’t wait until release to do code reviews. </li></ul><ul><li>Do team code reading regularly during development. </li></ul><ul><li>Experienced developers can lead by example. </li></ul><ul><li>Novice developers learn from the group. </li></ul><ul><li>Problems can be discovered early. </li></ul><ul><li>Good techniques can be shared early. </li></ul>
  22. 32. Two Kinds of Optimization <ul><li>Streamlining </li></ul><ul><ul><li>Algorithm replacement </li></ul></ul><ul><ul><li>Work avoidance </li></ul></ul><ul><ul><li>Code removal </li></ul></ul><ul><ul><li>These things are always good to do </li></ul></ul><ul><li>Special Casing </li></ul><ul><ul><li>Adds cruft, increases code size </li></ul></ul><ul><ul><li>Should only be done when proven necessary </li></ul></ul>
  23. 33. Avoid unnecessary displays or animation. <ul><li>Everything costs. </li></ul><ul><li>Wow costs. </li></ul><ul><li>As the number of widgets on the page increases, overall ongoing performance gets worse. </li></ul>
  24. 34. Only speed up things that take a lot of time. Speeding up things that take very little time will yield very little improvement.
  25. 35. Only speed up things that take a lot of time. <ul><li>If profiling shows that you are spending most of your time in A, don't bother optimizing C. </li></ul>A B C D
  26. 36. Improving performance <ul><li>If JavaScript were infinitely fast, most pages would run at about the same speed. </li></ul><ul><li>The bottleneck tends to be the DOM interface. </li></ul><ul><li>There is a significant cost every time you touch the DOM tree. </li></ul><ul><li>Each touch can result in a reflow computation, which is expensive. </li></ul>
  27. 37. Touch lightly <ul><li>It is faster to manipulate new nodes before they are attached to the tree. </li></ul><ul><li>Touching unattached nodes avoids the reflow cost. </li></ul><ul><li>Setting innerHTML does an enormous amount of work, but browsers are really good at it, and it only touches the DOM once. </li></ul>
  28. 38. Make good use of Ajax Libraries Effective code reuse will make widgets more effective.
  29. 39. How IE8 Spends Its Time <ul><li>Average time allocation of the Alexa 100: </li></ul>Layout 43.16% Rendering 27.25% HTML 2.81% Marshalling 7.34% DOM 5.05% Format 8.66% JScript 3.23% Other 2.5%
  30. 40. How IE8 Spends Its Time <ul><li>Opening a thread in GMail: </li></ul>Layout 9.41% Rendering 9.21% HTML 1.57% Marshalling 7.85% DOM 12.47% Format 38.97% JScript 14.43% Other 3.72%
  31. 41. Coding Efficiency <ul><li>Common subexpression removal. </li></ul><ul><li>Loop invariant removal. </li></ul><ul><li>Most compilers in most programming languages do these optimizations for you. </li></ul><ul><li>But not JavaScript. </li></ul>
  32. 42. Before <ul><li>var i; </li></ul><ul><li>for (i = 0; i < divs.length ; i += 1) { </li></ul><ul><li>divs[i].style. color = &quot;black&quot;; </li></ul><ul><li>divs[i].style. border = thickness + </li></ul><ul><li>'px solid blue' ; </li></ul><ul><li>divs[i].style. backgroundColor = &quot;white&quot;; </li></ul><ul><li>} </li></ul>
  33. 43. After <ul><li>var border = thickness + 'px solid blue' , </li></ul><ul><li>nrDivs = divs.length , </li></ul><ul><li>ds , i; </li></ul><ul><li>for (i = 0; i < nrDivs ; i += 1) { </li></ul><ul><li>ds = divs[i].style ; </li></ul><ul><li>ds .color = &quot;black&quot;; </li></ul><ul><li>ds .border = border ; </li></ul><ul><li>ds .backgroundColor = &quot;white&quot;; </li></ul><ul><li>} </li></ul>
  34. 44. Strings <ul><li>Concatenation with + </li></ul><ul><ul><li>Each operation allocates memory </li></ul></ul><ul><ul><li>foo = a + b; </li></ul></ul><ul><li>Concatenate with array .join('') </li></ul><ul><ul><li>The contents of an array are concatenated into a single string </li></ul></ul><ul><ul><li>foo = [a, b].join(''); </li></ul></ul>
  35. 45. Don't Tune For Quirks <ul><li>Some browsers have surprising inefficiencies. </li></ul><ul><li>A trick that is faster on Browser A might be slower on Browser B. </li></ul><ul><li>The performance characteristics of the next generation may be significantly different. </li></ul><ul><li>Avoid short-term optimizations. </li></ul>
  36. 46. Don't Optimize Without Measuring <ul><li>Intuitions are often wrong. </li></ul><ul><ul><li>start_time = new Date().valueOf(); </li></ul></ul><ul><ul><li>code_to_measured(); </li></ul></ul><ul><ul><li>end_time = new Date().valueOf(); </li></ul></ul><ul><ul><li>elapsed_time = end_time - start_time; </li></ul></ul><ul><li>A single trial is unreliable. Timers can be off by as much as 15 msec. </li></ul><ul><li>Even accurate measurements can lead to wrong conclusions. </li></ul>
  37. 47. O (1) <ul><li>An operation that is performed only once is not worth optimizing. </li></ul>
  38. 48. O ( n ) <ul><li>An operation that is performed many times may be worth optimizing. </li></ul>Time n Slope: Time per Iteration Fixed time: Startup and cleanup
  39. 49. The Axis of Error <ul><li>Inefficiency </li></ul>Time n Inefficiency
  40. 50. The Axis of Error <ul><li>Frustration </li></ul>Time n Frustration
  41. 51. The Axis of Error <ul><li>Failure </li></ul>Time n Failure
  42. 52. O ( n ) Time n Slope: Time per Iteration
  43. 53. O ( n log n ) Time n
  44. 54. O ( n 2 ) <ul><ul><li>Generally not suitable for browser applications except when n is very small. </li></ul></ul>Time n
  45. 55. The most effective way to make programs faster is to make n smaller. Ajax allows for just-in-time data delivery.
  46. 56. The Wall.