{"id":18,"date":"2006-11-07T01:25:04","date_gmt":"2006-11-07T09:25:04","guid":{"rendered":"http:\/\/porkrind.org\/missives2\/?p=18"},"modified":"2015-02-20T22:18:53","modified_gmt":"2015-02-21T06:18:53","slug":"efficient-javascript-or-inefficient-browser","status":"publish","type":"post","link":"https:\/\/porkrind.org\/missives\/efficient-javascript-or-inefficient-browser\/","title":{"rendered":"Efficient JavaScript or Inefficient Browser?"},"content":{"rendered":"<p>I caught <a href=\"http:\/\/dev.opera.com\/articles\/view\/48\/\">this article<\/a> on <a href=\"http:\/\/reddit.com\">Reddit<\/a> this weekend and had definite mixed feelings about it. Many of their suggestions were obviously good ideas\u2014calling <tt>SetTimeout()<\/tt> with a string is obviously stupid, as are 90% of the possible uses of <tt>eval()<\/tt> and <tt>Function()<\/tt>. The advice on what causes the DOM to repaint and reflow was interesting and informative.<\/p>\n<p>It would have been a really good article except that some of their advice seemed weak. Some of what was masquerading as optimization &#8220;advice&#8221; seemed more like work-arounds for a substandard javascript implementation.<\/p>\n<p>Take this for example:<\/p>\n<blockquote>\n<pre><code class=\"language-javascript\">var min = Math.min(a,b);\r\nA.push(v);\r\n<\/code><\/pre>\n<p>These alternatives provide the same functionality, while performing better:<\/p>\n<pre><code class=\"language-javascript\">var min = a &lt; b ? a : b;\r\nA[A.length] = v;\r\n<\/code><\/pre>\n<\/blockquote>\n<p>Yes, I can see in someone&#8217;s world that might perform better. But come on, the compiler\/scanner should be able to produce the equivalent code. Anything less is a failing of your browser and not an &#8220;unoptimized&#8221; program. Do you really expect people to crappify their programs because your browser can&#8217;t properly optimize javascript for you?<\/p>\n<p>&#8220;Well,&#8221; I hear you say, &#8220;it&#8217;s <em>interpreted<\/em>, not <em>compiled<\/em>.&#8221; To which I say, yeah, well there&#8217;s your first problem. Fix that before telling me that I need to use stupid constructs to make my program go fast.<\/p>\n<p>Here&#8217;s another example:<\/p>\n<blockquote><p>This example requires the script engine to create 21 new string objects, once for each time the length property is accessed, and once each time the charAt method is called:<\/p>\n<pre><code class=\"language-javascript\">var s = '0123456789';\r\nfor( var i = 0; i &lt; s.length; i++ ) {\r\n  s.charAt(i);\r\n}<\/code><\/pre>\n<p>This equivalent example creates just a single object, and will perform better as a result:<\/p>\n<pre><code class=\"language-javascript\">var s = new String('0123456789');\r\nfor( var i = 0; i &lt; s.length; i++ ) {\r\n  s.charAt(i);\r\n}\r\n<\/code><\/pre>\n<\/blockquote>\n<p>Again, this strikes me as being a failing of the compiler. I see no reason why the browser should instantiate a string object just to call its length(). A little bit of hard coding in your implementation can optimize that to a <tt>strlen()<\/tt> call or better\u2014No object instantiation at all. What if someone overrode String.prototype.length()? That&#8217;s one <tt>if<\/tt> statement in C and some fallback code. Again, I don&#8217;t think <em>my<\/em> program should suffer because <em>your<\/em> browser doesn&#8217;t properly optimize things.<\/p>\n<p>But then again, maybe I&#8217;m just crazy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I caught &#8220;this article&#8221;:http:\/\/dev.opera.com\/articles\/view\/48\/ on &#8220;Reddit&#8221;:http:\/\/reddit.com this weekend and had definite mixed feelings about it. Many of their suggestions were obviously good ideas. But some of what was masquerading as optimization &#8220;advice&#8221; seemed more like work-arounds for a substandard javascript implementation.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[13],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-software","tag-javascript"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":8,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":495,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts\/18\/revisions\/495"}],"wp:attachment":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}