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.

Node.js: Some quick optimization advice

203 pointsby SanderMakover 9 years ago

15 comments

mixonicover 9 years ago
This is a good thing to raise awareness of, but the solution is poor advice.<p>Asking developers to remember the &quot;gotcha&quot; of 600 characters is not really viable. Instead, if this is important to you, consider the addition of minification or comment stripping to your production deployment process. Minification will also make the variable names and syntax use shorter, saving you further precious characters.<p>Learn the problem, but automate the solution.
评论 #10376822 未加载
dapover 9 years ago
This is a microbenchmark. It&#x27;s a tight loop that calls the function 500 million times. The function itself just adds two numbers. It&#x27;s pretty close to the best possible improvement for inlining a function. If that&#x27;s what your program does, and it&#x27;s a big part of what your program does, then inlining it may be a big performance win. Even then, addressing this may not be a worthwhile tradeoff. Even then, as others have pointed out, you could use a minimizer.<p>If your program is not CPU-bound, or if it doesn&#x27;t have a function that&#x27;s called millions of times in a tight loop, or if that doesn&#x27;t make up the majority of time spent on CPU, or if that function does more than execute a couple of instructions, then the performance difference will likely be enormously less.
评论 #10376057 未加载
pauljzover 9 years ago
If you&#x27;re wondering why comments are a factor at all, and aren&#x27;t just discarded by the lexer, remember that comments in JS are preserved and available to things like `Function.prototype.toString()`. I&#x27;ve seen this used to do evil multiline string support a few times. Slap the multiline string or template into a comment inside a function and then have another function that toStrings it and strips the boilerplate.<p>This sounds like a horrifying hack but it&#x27;s also pretty similar to how Angular&#x27;s shorthand DI works.
评论 #10375554 未加载
评论 #10375846 未加载
constexprover 9 years ago
The bug has been marked as WontFix by the V8 team: <a href="https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;v8&#x2F;issues&#x2F;detail?id=3354" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;v8&#x2F;issues&#x2F;detail?id=3354</a>. It looks like they are considering fixing it for TurboFan and not for Crankshaft, but I&#x27;m not sure what that means. Does that only apply to asm.js code?
评论 #10375488 未加载
spullaraover 9 years ago
Wow, the v8 optimizer can sometimes be a pretty blunt instrument. Shouldn&#x27;t it look at the size of the inlined function after it compiles it to some intermediate state that erases things like comments and names?
评论 #10375295 未加载
评论 #10375750 未加载
mhdover 9 years ago
One comment mentions a quite significant speedup when switching the variable initialization step of the for loop from let to var. As excepted, you&#x27;ll also get this if you keep the &quot;let&quot; keyword, but move it outside of the loop.<p>What kind of optimization step is prevented here?
tambourine_manover 9 years ago
Posted that yesterday, didn&#x27;t get much traction.<p>Whenever I begin to convince myself that JS and Node are actually fine languages &#x2F; environments to code on, some weird edge case like this pops up.<p>Not entirely sure if it&#x27;s just its popularity, which is bound to expose its rough corners, or if it&#x27;s fundamentally bad designed (written in 10 days in the late 90&#x27;s, etc)
评论 #10376919 未加载
评论 #10375947 未加载
评论 #10375737 未加载
评论 #10376238 未加载
评论 #10375606 未加载
评论 #10375948 未加载
bshimminover 9 years ago
This cheerfully reminded me of the thing in MRI Ruby of (relatively recent) yesteryear where strings longer than 23 characters were twice as slow, qv. <a href="http:&#x2F;&#x2F;patshaughnessy.net&#x2F;2012&#x2F;1&#x2F;4&#x2F;never-create-ruby-strings-longer-than-23-characters" rel="nofollow">http:&#x2F;&#x2F;patshaughnessy.net&#x2F;2012&#x2F;1&#x2F;4&#x2F;never-create-ruby-strings...</a>
vivramover 9 years ago
Good to know as a &quot;gotcha&quot; but typically when you get to an optimization like this you should have covered all other bigger optimizations like minification, ... that this won&#x27;t actually return big yields.
vvoyerover 9 years ago
Who need this? Prople writing really really performant code like parsers. Who write parsers? 1% of ppl? Sad this get so much exposure. Stop losing time with micro optimization, this is a bug that&#x27;s it.
746F7475over 9 years ago
I&#x27;m at work and can not test this out, but does adding whitespaces increase the function length here?
ameliusover 9 years ago
Isn&#x27;t there a way to force the inline optimization of a function, regardless of the character count?
m1sta_over 9 years ago
I had this exact thing happen to me. It drove me crazy. Thank you!
WorldWideWayneover 9 years ago
So, is this is a point in favor of <i>not</i> commenting your Javascript code or using inline docs?<p>Historically, I preferred to use inline JsDoc style comments as the source of documentation for my public APIs. Recently though, I decided that I didn&#x27;t like them and that I wanted something better. I was hoping to find some tool that parses my JS to AST, figures out what was being exported (e.g. what was public) and writes a JSON document that I could diff over time, to figure out what was documented and what wasn&#x27;t (in my external docs). So far, I have found this project called `doctor` which sounded close to what I&#x27;m looking for, but it still relies on comments ~ <a href="https:&#x2F;&#x2F;github.com&#x2F;jdeal&#x2F;doctor" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jdeal&#x2F;doctor</a> ~ So, I might have to write it myself using something like Esprima to get the AST...
评论 #10375629 未加载
评论 #10375581 未加载
评论 #10375577 未加载
评论 #10375620 未加载
bru_over 9 years ago
At first I thought the comment was actually serious and couldn&#x27;t believe it before I read the whole article. Thanks for pointing this out