A place to cache linked articles (think custom and personal wayback machine)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 5 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. title: The Cost of Frameworks
  2. url: https://aerotwist.com/blog/the-cost-of-frameworks/
  3. hash_url: 8d56b85a75797f70d0abe8a9f7cec2d6
  4. <p><strong>Update: Nov 16th 2015 - Added an extra row in the table for React under production conditions. The good news: it’s 3x slower than vanilla, yes, but in actual terms I’d say it’s fast for TodoMVC! The Polymer TodoMVC sample was also updated to version 1.2.2 today, and that, too, is faster.</strong></p>
  5. <p>If you prefer watching to reading, here’s the video of the talk (you can also <a href="https://speakerdeck.com/paullewis/framework-here-its-the-bestestest">get the slides</a>, too, if you like):</p>
  6. <iframe width="100%" height="420" src="//www.youtube.com/embed/_yCz1TA0EL4?vq=hd720" frameborder="0" allowfullscreen="allowfullscreen" style="margin-bottom: 20px; box-shadow: 0 2px 2px rgba(0,0,0,0.3)">
  7. </iframe>
  8. <p>If you prefer reading to watching, well, keep reading…</p>
  9. <h2 id="the-benefits-of-frameworks">The benefits of frameworks</h2>
  10. <p>Earlier in the year I wrote about <a href="https://aerotwist.com/react-plus-performance-equals-what/">React’s performance characteristics</a> as the tree size it has to manage gets larger (TL;DR the bigger the tree, the more computation work it has to do). The responses I got to that post really varied, from appreciative and constructive to, well, the other end of the spectrum. The responses gave me some additional insight, though, which I think applies to all frameworks (to a greater or lesser extent), and which I’ll now try and summarize:</p>
  11. <ul>
  12. <li><strong>Frameworks are fun to use</strong>. I don’t think I know any developer who actively seeks out a more challenging and less fun experience! And yes, absolutely, ergonomics <em>are</em> important.</li>
  13. <li><strong>Frameworks make it quicker to build an MVP</strong>. This one is important, particularly for smaller, fledgling organizations or companies. Having a fast bootstrap may be the difference between shipping and not shipping.</li>
  14. <li><strong>Frameworks work around lumps / bugs in the platform</strong>. I think this is less of an issue these days (though not irrelevant) as browsers have rallied around standards very well in the past few years. Certainly this is less painful compared to my early career, where we were dealing with IEs 5 and 6, and early versions of Firefox, Safari and, of course, Chrome.</li>
  15. <li><strong>Knowing [insert framework] means I get paid.</strong> If you’re looking to get hired, saying that you know the latest and greatest framework(s) may be the difference maker.</li>
  16. </ul>
  17. <blockquote>
  18. <p>The key message I heard over and over, sometimes explicitly, and often implicitly, is that ergonomics are the most important factor for many developers.</p>
  19. </blockquote>
  20. <p>The key message I heard over and over, sometimes explicitly, and often implicitly, is that ergonomics are <em>the most important factor</em> for many developers. If I can paraphrase, the sentiment was “if I have an easier time I’ll build something better for my users”. At face value that sounds appealing, but I think it misses a lot of important points.</p>
  21. <h2 id="developer-ergonomics-vs-user-needs">Developer Ergonomics vs User Needs</h2>
  22. <p>Users have needs, too, though. Here are a few of them that come to mind:</p>
  23. <ul>
  24. <li><strong>Sites and apps should load quickly.</strong></li>
  25. <li><strong>They should have smooth interactions.</strong></li>
  26. <li><strong>They shouldn’t slow down my phone.</strong></li>
  27. <li><strong>They shouldn’t crash.</strong></li>
  28. <li><strong>They should have features I want.</strong></li>
  29. </ul>
  30. <p>So immediately we’re into a potential trade-off situation: on the one hand we have our convenience, our ergonomics, and on the other hand we have our users’ needs. And we have to trade, whether we like it or not, since frameworks aren’t free.</p>
  31. <h2 id="a-quick-side-step">A quick side step</h2>
  32. <p>At this point I’m going to park libraries, and leave them out of this discussion. The reason is that, in my view at least, libraries can be swapped out and replaced if they prove problematic. Don’t like the way a library does date formatting? No problem, just switch it for something else. Frameworks, on the other hand, tend to be a lot more difficult to swap, often requiring a rebuild of the app in question. And, by their nature, they’re a lot bigger and more involved.</p>
  33. <h2 id="the-cost-of-code">The cost of code</h2>
  34. <p>All code has costs, but I think there are particular costs that are unique to frameworks.</p>
  35. <figure>
  36. <img src="https://aerotwist.com/static/blog/the-cost-of-frameworks/deprecated.jpg" alt="A deprecation warning." />
  37. <figcaption>That magical moment where an framework tells you something is deprecated. Apparently it was superseded by Java. Weird.</figcaption>
  38. </figure>
  39. <ul>
  40. <li><strong>Learning it.</strong> You have to take time out to learn any new framework: what it means to write “idiomatic” code for that framework; its patterns, practices, and ways of doing things. This is no different to the web platform itself. There’s no free ride, you’re just swapping one set of issues for another.</li>
  41. <li><strong>Re-learning it.</strong> One day you’re just bimbling along, developing your app, and in the console you see a warning telling you that there’s been a deprecation, and that you need to update your code. That often requires figuring out what the latest version of the framework needs you to do differently, and that may also mean updating projects that shipped with older versions of the framework.</li>
  42. <li><strong>Debugging it.</strong> Frameworks, like any software, have bugs. That’s to be expected, and it wouldn’t be any different if you went vanilla and wrote your own. Debugging a framework, however, often ranges from difficult to impossible. If you find the issue, you may have to keep your own frankensteined version around with a monkey patch applied until the real fix lands in the framework’s production build.</li>
  43. </ul>
  44. <p>Aside from the developer costs, there are costs to the user, too:</p>
  45. <ul>
  46. <li><strong>Time.</strong> How long does it take to bootstrap the thing we sent down the wire?</li>
  47. <li><strong>Bandwidth.</strong> How much bandwidth does it use up?</li>
  48. <li><strong>CPU usage (battery).</strong> How CPU intensive is it? CPU utilization is going to correlate to battery usage: burn the CPU and the battery will get drained.</li>
  49. <li><strong>Frame rate.</strong> How well does the code update the screen? (We know that <a href="https://paul.kinlan.me/what-news-readers-want/">users like smooth interactions</a>.)</li>
  50. <li><strong>Memory usage.</strong> How much memory does it use? Will it cause other apps to be evicted from memory? Perhaps even crash a tab?</li>
  51. </ul>
  52. <h2 id="bringing-data-to-the-table">Bringing data to the table</h2>
  53. <p>So with all that in mind, I wanted to start off a conversation about measuring some of those costs, and my hope is that together we can build up more of a picture about the trade-offs we choose to make.</p>
  54. <p>I decided to take a look at the bootstrapping time for frameworks on mobile. The theory being that, given a like-for-like app, one should be able to see how long it takes each framework to boot. I opted to use <strong>TodoMVC</strong> for the test, because in my view it’s an MVP web app, and from a user’s point of view, each variant is functionally identical.</p>
  55. <figure>
  56. <img src="https://aerotwist.com/static/blog/the-cost-of-frameworks/todomvc.jpg" alt="TodoMVC." />
  57. <figcaption>TodoMVC: source of my bootstrapping test.</figcaption>
  58. </figure>
  59. <h3 id="bootstrapping--time-to-interactive">Bootstrapping / Time-to-interactive</h3>
  60. <p>Of the costs listed above, I decided to take a look at the <strong>Time</strong>, <strong>Bandwidth</strong>, and <strong>CPU usage</strong> for booting up some frameworks on a Nexus 5 and iPhone 5S.</p>
  61. <figure>
  62. <img src="https://aerotwist.com/static/blog/the-cost-of-frameworks/time-to-interactive.jpg" alt="Measuring time to interactive." />
  63. <figcaption>Time to interactive: measuring how long it takes for an app to get to a state where someone can interact with it.</figcaption>
  64. </figure>
  65. <p>I decided to measure the time it takes to <strong>evaluate and execute each framework’s initial payload of JavaScript</strong>, and the processing and setup of the initial data set into a model. I decided to ignore the cost of styles, layout, paint and so on since they shouldn’t vary all that much between the different TodoMVC implementations. I was also not testing transfer time.</p>
  66. <h3 id="methodology">Methodology</h3>
  67. <p>I used <a href="https://webpagetest.org">WebPagetest</a> to load the pages on a Nexus 5, and I requested a timeline file for each run, which I then passed to <a href="https://aerotwist.com/bigrig/">Big Rig</a> for processing. The iPhone results I did myself, and had to tally up manually, because unfortunately Safari’s JavaScript profiling doesn’t seem to support any timeline / trace file export.</p>
  68. <figure>
  69. <img src="https://aerotwist.com/static/blog/the-cost-of-frameworks/bigrig.jpg" alt="Using Big Rig to measure TodoMVC bootup time." />
  70. <figcaption>Using Big Rig to quickly assess the TodoMVC bootup time per framework.</figcaption>
  71. </figure>
  72. <p>This, incidentally, is why I built <a href="https://aerotwist.com/bigrig">Big Rig</a>. I wanted to make it easier for all of us to start processing this kind of data quickly.</p>
  73. <h3 id="repeating-the-test">Repeating the test</h3>
  74. <p>If you want to do your own test like this, at least for a device running Chrome, you should do the following:</p>
  75. <ul>
  76. <li>Install the Big Rig CLI: <code>npm install -g bigrig</code>.</li>
  77. <li>Go to <a href="http://webpagetest.org">WebPagetest</a>.</li>
  78. <li>Set the location to Dulles.</li>
  79. <li>Set the browser to Nexus 5 - Chrome (or Beta/Dev).</li>
  80. <li>Under the Chrome settings, check “Capture Dev Tools Timeline”.</li>
  81. <li>Run the test.</li>
  82. <li>Download the Timeline file on the left hand side of the results.</li>
  83. <li>Run <code>bigrig --file /path/to/timeline.json --pretty-print</code>.</li>
  84. </ul>
  85. <p>If you prefer, you can watch me getting a Timeline file from WebPagetest:</p>
  86. <iframe width="100%" height="420" src="//www.youtube.com/embed/LFOCkLqPklg?vq=hd720" frameborder="0" allowfullscreen="allowfullscreen" style="margin-bottom: 20px; box-shadow: 0 2px 2px rgba(0,0,0,0.3)">
  87. </iframe>
  88. <h2 id="the-results">The results</h2>
  89. <p>Here’s how the various frameworks stacked up in my tests:</p>
  90. <table class="left-highlight">
  91. <thead>
  92. <tr>
  93. <th>Framework</th>
  94. <th>Size</th>
  95. <th>Bootstrap time N5<sup>1,3</sup></th>
  96. <th>Bootstrap time iPhone 5S<sup>2,3</sup></th>
  97. </tr>
  98. </thead>
  99. <tbody>
  100. <tr>
  101. <td>Polymer v1.1.4</td>
  102. <td>41KB<sup>5</sup></td>
  103. <td>409ms</td>
  104. <td>233ms</td>
  105. </tr>
  106. <tr>
  107. <td>Polymer v1.2.2</td>
  108. <td>47KB<sup>5</sup></td>
  109. <td>155ms</td>
  110. <td>232ms</td>
  111. </tr>
  112. <tr>
  113. <td>AngularJS v1.4.3</td>
  114. <td>324KB</td>
  115. <td>518ms</td>
  116. <td>307ms</td>
  117. </tr>
  118. <tr>
  119. <td>React v0.13.3 [JSX not transformed]</td>
  120. <td>311KB</td>
  121. <td>1,201ms</td>
  122. <td>1,463ms</td>
  123. </tr>
  124. <tr>
  125. <td>React v0.13.3 [JSX transformed via Babel]<sup>4</sup></td>
  126. <td>162KB</td>
  127. <td>509ms</td>
  128. <td>282ms</td>
  129. </tr>
  130. <tr>
  131. <td>React v0.13.3 [JSX transformed; prod build]<sup>4, 6</sup></td>
  132. <td>160KB</td>
  133. <td>174ms</td>
  134. <td>118ms</td>
  135. </tr>
  136. <tr>
  137. <td>Backbone v1.2.2 [inc. jQuery &amp; Underscore]</td>
  138. <td>139KB</td>
  139. <td>248ms</td>
  140. <td>168ms</td>
  141. </tr>
  142. <tr>
  143. <td>Ember v1.10.0-beta.3</td>
  144. <td>580KB</td>
  145. <td>1,992ms</td>
  146. <td>1,440ms</td>
  147. </tr>
  148. <tr>
  149. <td>Vanilla</td>
  150. <td>16KB</td>
  151. <td>50ms</td>
  152. <td>33ms</td>
  153. </tr>
  154. </tbody>
  155. </table>
  156. <ol>
  157. <li>Tests done on a Nexus 5 running Chrome 47.</li>
  158. <li>Tests done on an iPhone 5S running Safari 9.</li>
  159. <li>All bootstrapping time includes handling initial todo list data.</li>
  160. <li>JS Transformer stripped; JSX files transformed via Babel.</li>
  161. <li>Excludes Web Components Polyfill (38KB).</li>
  162. <li>React switched for minified production build.</li>
  163. </ol>
  164. <p>For me the results are pretty clear: <strong>there appears to be a pretty hefty tax to using Frameworks on mobile, especially compared to writing vanilla JavaScript</strong>. The fastest is React (under production conditions), which is awesome, but it’s still 3x slower than Vanilla (and I <a href="https://aerotwist.com/react-plus-performance-equals-what/">still have concerns over its scaling properties</a>.)</p>
  165. <p>To make things super clear, here are a couple of notes:</p>
  166. <ul>
  167. <li><strong>TodoMVC doesn’t JSX Transform React, so I did.</strong> The reason there are three entries for React is that the TodoMVC example doesn’t transform JSX, and instead includes the JSX Transformer library. To make things fairer (because the runtime JSX transformation is expensive!), I made a more ‘productionized’ version of the example, and tested that, too. The downside is that it’s not the minified version of React, which is different again. So I also switched out the build of React for the production version, and that’s the third build in the table.</li>
  168. <li><strong>These times do <em>not</em> include transfer time.</strong> The only thing I was measuring was the time in JavaScript to bootstrap the framework and get the initial view built. In fact, <em>some of the frameworks in TodoMVC aren’t minified</em>. For a more discussion on framework transfer sizes, <a href="https://www.filamentgroup.com/lab/mv-initial-load-times.html">check out the Filament Group’s post from last year</a>.</li>
  169. </ul>
  170. <h3 id="possible-objections">Possible Objections</h3>
  171. <p>I figure there’s likely to be some objections to the test, which are definitely worth talking through:</p>
  172. <blockquote>
  173. <p>I’d argue your use-case is probably more expensive than TodoMVC.</p>
  174. </blockquote>
  175. <ul>
  176. <li><strong>“TodoMVC isn’t idiomatic.”</strong> I checked this one out, and from what I understand every implementation <em>is</em> idiomatic, and framework experts file PRs against them when they aren’t.</li>
  177. <li><strong>“TodoMVC isn’t my use-case.”</strong> That’s probably true, but I’d argue your use-case is probably <em>more</em> expensive than TodoMVC. <a href="https://github.com/reddit/reddit-mobile/issues/247">Paul Irish recently did a performance audit on Reddit’s mobile site</a>, and he discovered, amongst other things, that 22 seconds were going into just booting up React’s components on mobile!</li>
  178. <li><strong>“A Nexus 5 / iPhone 5S isn’t what our users use.”</strong> That’s also probably true. I have the privilege of using good hardware, and I would imagine that many people don’t have access to top end phones, so I’d expect these numbers to be even worse in those cases.</li>
  179. <li><strong>“It’ll be better in the next version of &lt;insert framework here&gt;.”</strong> This may well be true, but it doesn’t do much for anything you’ve shipped with the current version.</li>
  180. </ul>
  181. <h2 id="the-64k-question">The $64k question</h2>
  182. <p>So, inevitably, the question comes: <em>“Should you use a framework?”</em></p>
  183. <p>I can’t answer that question, because I think it’s entirely your call. There are a million and one reasons why you may feel you need to use one. But, for what it’s worth, here are my thoughts:</p>
  184. <ul>
  185. <li><strong>Frameworks contribute ideas and concepts</strong>. Frameworks are a crucial part of understanding what approaches work, and what don’t, and that ultimately drives improvements at the platform level. In that regard I think they serve as important testing grounds for the platform changes of tomorrow and let us figure things out before they become embedded in the web permanently.</li>
  186. <li><strong>Frameworks are an inversion of control</strong>. The reason I parked libraries earlier on is that you can swap them out. Frameworks, on the other hand, invert control. <em>They</em> control the lifecycle of the app, and give <em>you</em> entry points where your code runs. You’re still responsible for the final code, but you’re not in control.</li>
  187. <li><strong>Frameworks are expensive on mobile</strong>. Compared to vanilla, at least. For me, prohibitively so, but everyone has their own limits of what they’re willing to tolerate.</li>
  188. </ul>
  189. <blockquote>
  190. <p>Investing in knowledge of the web platform itself is the best long-term bet.</p>
  191. </blockquote>
  192. <p>As I look at frameworks, I see the ergonomic benefits (and those are important, I agree!), but I can’t help but feel that, for many developers, <strong>investing in knowledge of the web platform itself is the best long-term bet</strong>. Frameworks come and go, it just seems to be the ebb and flow of the web, and, as I said above, they <em>do</em> contribute ideas and patterns. But if you ever find that the one you use no longer works for you, or has a bug that remains unfixed, being able to understand the platform that underpins it will help enormously.</p>
  193. <h2 id="the-bigger-conversation">The bigger conversation</h2>
  194. <p>When I wrote the post earlier in the year about React, I said this:</p>
  195. <blockquote class="quote">
  196. <p>It seems to me that developer ergonomics should be less important than our users’ needs.</p>
  197. </blockquote>
  198. <p>I still believe that. As much as I love the idea of having an easier life, I don’t want to ship something to people that doesn’t run well, and I don’t want them to pay the cost. Today I’m concerned by the costs of booting up frameworks on mobile.</p>
  199. <blockquote>
  200. <p>I’m concerned by the costs of booting up frameworks on mobile.</p>
  201. </blockquote>
  202. <p>This is only a first step. There are other metrics besides bootstrapping I’ve barely had chance to look into: memory usage, long-term CPU usage, and impact on frame rate to name three. Overall, I think we have more to do to properly assess the impact of code that we ship to our users and the costs we pass on to them.</p>
  203. <p>If we can achieve fast-booting, low-memory, smooth-executing frameworks with good ergonomics we’ll be onto a winner. Until then, for mobile at least, I’ll be sticking to the vanilla web platform.</p>