TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Don't use CoffeeScript

33 pointsby L8Dover 11 years ago

11 comments

amasadover 11 years ago
This is actually almost completely wrong information. The lower-level for loop is much more JS engine friendly than the Array methods (filter, map etc.). So unless you write JS like it&#x27;s C, for the most part, CoffeeScript will probably generate more efficient code[1]. As shown in the jsperf by @mischani [2]<p>The only thing that&#x27;s true is that CofeeScript generates some throwaway code because of implicit returns and the &quot;everything is an expression&quot; rule. However, I doubt that they would cause much overhead, specially with modern JS engines optimisation capabilities.<p>[1]: <a href="http://mrale.ph/blog/2011/11/05/the-trap-of-the-performance-sweet-spot.html" rel="nofollow">http:&#x2F;&#x2F;mrale.ph&#x2F;blog&#x2F;2011&#x2F;11&#x2F;05&#x2F;the-trap-of-the-performance-...</a><p>[2]: <a href="http://jsperf.com/don-t-use-coffeescript" rel="nofollow">http:&#x2F;&#x2F;jsperf.com&#x2F;don-t-use-coffeescript</a>
评论 #6350399 未加载
mischanixover 11 years ago
Turns out the comprehension is faster on my setup:<p><a href="http://jsperf.com/don-t-use-coffeescript" rel="nofollow">http:&#x2F;&#x2F;jsperf.com&#x2F;don-t-use-coffeescript</a>
评论 #6343793 未加载
评论 #6343832 未加载
评论 #6343769 未加载
评论 #6350292 未加载
评论 #6343740 未加载
rzimmermanover 11 years ago
There&#x27;s no evidence presented here that there&#x27;s any significant (or even negative) performance impact from the way the CoffeeScript compiles these list comprehensions. Also, if your performance is constrained by a list comprehension, there&#x27;s nothing in CoffeeScript preventing you from using &quot;Array.filter&quot; instead. By and large CoffeeScript compiles one-to-one with JavaScript and when it doesn&#x27;t, the resulting code is at least reasonably performant.<p>I do agree that the default return behavior is probably a bad idea. It probably came from Ruby, and I can see the appeal, but I&#x27;ve often created bugs by accidentally using the implicit return, then adding something to the end of a function. It&#x27;s not really that big of a deal - I just as a policy always use an explicit return.
city41over 11 years ago
Also as an aside, CoffeeScript will only bother to populate the results array if the comprehension is the last thing in the function. If there is anything else that follows and you never use the result of the comprehension, then it compiles into a simple for loop. This is because CoffeeScript can&#x27;t know if you actually plan to take advantage of its implicit return in this case.<p>I have found I&#x27;ve been forced to add a dummy `return` at the end of some of my CS methods because of this. It&#x27;s one of only a very few complaints I have with the language.
hardwaresoftonover 11 years ago
Hey quick question, maybe you should use map instead of filter?<p><a href="http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=117" rel="nofollow">http:&#x2F;&#x2F;www.dotnetspeaks.com&#x2F;DisplayArticle.aspx?ID=117</a> (random site with example)<p>map is the functional equivalent of what you&#x27;re trying to do there, not filter.
评论 #6343673 未加载
cubicle67over 11 years ago
fwiw, I love coffeescript. I write a game a few weeks ago in it, but I&#x27;ve never shown it to anyone before. Have a look here if you&#x27;re interested <a href="http://quietcode.com/vectroid" rel="nofollow">http:&#x2F;&#x2F;quietcode.com&#x2F;vectroid</a><p>I&#x27;ve made no attempts at all at performance tuning yet it runs very nicely in webkit browsers and not too bad in firefox.
评论 #6343806 未加载
评论 #6344337 未加载
评论 #6343818 未加载
评论 #6344127 未加载
评论 #6343908 未加载
评论 #6344117 未加载
评论 #6343843 未加载
rcohover 11 years ago
It seems that what you actually want is for CoffeeScript to have configurable backends. I agree that it&#x27;s silly to generate IE6 compatible code when I know that I&#x27;m targeting node. This isn&#x27;t a reason not to use CoffeeScript -- it&#x27;s a reason to improve CoffeeScript.<p>Furthermore, as others have noted, your post does smack of premature optimization. Without numbers, most of the claims are all pretty meaningless. Judging by number of instructions is no longer a valid metric on any platform on any level of abstraction and hasn&#x27;t been since the 1980s.
smoyerover 11 years ago
I like CoffeeScript and will continue to use it where appropriate.<p>But I do wonder why we&#x27;re stuck with all the cruft to let ECMAScript be backwards compatible. Couldn&#x27;t we just one time introduce a few breaking changes to clean up the mistakes in the language&#x27;s design?<p>And if you&#x27;re really opposed to that, how about the same strict versus transitional semantics we use for HTML? I&#x27;d be happy to be writing code in a clean strict subset of JavaScript.
评论 #6343935 未加载
rpwilcoxover 11 years ago
The great thing about there being so many Javascript transpilers these days is that you can pick the right language to suit your needs.<p>Really want &#x2F; need Google Closure &#x2F; the latest ECMAScript stuff? ClojureScript supports these things pretty well.<p>Really like Ruby (or Python) but find yourself in Javascript land? Coffeescript is right for you.<p>There&#x27;s some interesting buzz around getting Coffescript more Google Closure compliant, which is probably the better way to go (assuming your targeting front end JS, not Node).<p>But yes, there are certainly places where Coffeescript generates a crap-ton of code, where if you can pinpoint your Javascript runtime (like in Node&#x27;s case) you could do more, better, and faster.
tlrobinsonover 11 years ago
This person seems to think that &quot;less code&quot; is &quot;faster code&quot;.<p><i>Yes, I do have to give some credits to putting the number x number directly into the push call</i><p>Are they really suggesting<p><pre><code> _results.push(number * number); </code></pre> is significantly faster than, say<p><pre><code> var result = number * number; _results.push(result); </code></pre> Because it&#x27;s not.<p>Though I do wonder why CoffeeScript doesn&#x27;t do something like this to avoid the function call:<p><pre><code> _results[_i] = number * number;</code></pre>
tallesover 11 years ago
Yet Another Coffeescript Rant