Your SlideShare is downloading. ×
0
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Ajax Performance
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ajax Performance

1,822

Published on

http://yuiblog.com/blog/2008/12/23/video-crockford-performance/

http://yuiblog.com/blog/2008/12/23/video-crockford-performance/

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,822
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
64
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  1. JavaScript: The Good Parts Part Six: Ajax Performance Douglas Crockford [email_address]
  2. Memento
  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. “ When your only tool is a hammer, every problem looks like a webpage.”
  5. The Ajax Revolution The page is an application with a data connection to a server.
  6. When the user does something, we send a JSON message to the server, and receive a JSON message as the result.
  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.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. Division of Labor How is the application divided between the browser and the server?
  19. Pendulum of Despair Server The browser is a terminal.
  20. Pendulum of Despair Server Browser The browser is a terminal The server is a file system.
  21. Seek the Middle Way. A pleasant dialogue between specialized peers.
  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>
  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>
  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>
  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>
  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>
  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>
  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>
  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>
  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>
  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>
  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>
  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>
  34. Only speed up things that take a lot of time. Speeding up things that take very little time will yield very little improvement.
  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
  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>
  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>
  38. Make good use of Ajax Libraries Effective code reuse will make widgets more effective.
  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%
  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%
  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>
  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>
  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>
  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>
  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>
  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>
  47. O (1) <ul><li>An operation that is performed only once is not worth optimizing. </li></ul>
  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
  49. The Axis of Error <ul><li>Inefficiency </li></ul>Time n Inefficiency
  50. The Axis of Error <ul><li>Frustration </li></ul>Time n Frustration
  51. The Axis of Error <ul><li>Failure </li></ul>Time n Failure
  52. O ( n ) Time n Slope: Time per Iteration
  53. O ( n log n ) Time n
  54. O ( n 2 ) <ul><ul><li>Generally not suitable for browser applications except when n is very small. </li></ul></ul>Time n
  55. The most effective way to make programs faster is to make n smaller. Ajax allows for just-in-time data delivery.
  56. The Wall.

×