TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What are the hidden performance tricks for JavaScript?

10 点作者 chilling大约 1 年前
I am currently working on a heavy JS-based tool where every millisecond counts. I&#x27;ve been searching for lesser-known JavaScript performance tricks that go beyond typical optimizations like using web workers or removing unused code. It feels like uncovering hidden knowledge.<p>One discovery I&#x27;ve made is that using new Array is significantly faster than Array.from or push()[0].<p>Could you share any similar insights or lesser-known performance optimizations you&#x27;ve come across?<p>[0] https:&#x2F;&#x2F;www.measurethat.net&#x2F;Benchmarks&#x2F;ShowResult&#x2F;506720

10 条评论

surprisetalk大约 1 年前
A few general suggestions:<p>• Learn Rust! When I learned how memory worked, it made me a much better JS dev.<p>• Check out Casey Muratori&#x27;s courses on performance. Some are free on YouTube.<p>• Start from scratch with a data-oriented approach. What bits do you need to move around, and why? What&#x27;s the best way to store and shape those bits?<p>• Can&#x2F;should you use WASM instead?
kypro大约 1 年前
If working with number arrays using Float32Array can provide significant improvements to performance.<p>Never use .forEach(), always use a for loop. Same applies to methods like .map().<p>Some calculations are just much more expensive to do than others. Exponentiation for example is much more expensive than multiplication. You want to write your code in a way that not only considers the total number of operations, but also minimises expensive operations.<p>Caching can help if you&#x27;re doing repetitive computations.<p>If you&#x27;re absolutely pushing performance to the max you will probably want to look at doing some clever stuff with WebGL2, WASM or Web Workers. Wherever you can avoid this though I would recommend it because it will make your code much more complicated and won&#x27;t even necessarily out perform because of overheads.<p>Edit: Also check out this guy on YT, <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=WLwTlC1R2sY" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=WLwTlC1R2sY</a> He does some really interesting deep dives into JS performance.
评论 #39586184 未加载
solardev大约 1 年前
What is the actual operation you&#x27;re trying to optimize? What does the code need to do?
评论 #39586027 未加载
austin-cheney大约 1 年前
This was attempt to research the fastest possible approach to a JavaScript GUI in the browser.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;prettydiff&#x2F;wisdom&#x2F;blob&#x2F;master&#x2F;performance_frontend.md">https:&#x2F;&#x2F;github.com&#x2F;prettydiff&#x2F;wisdom&#x2F;blob&#x2F;master&#x2F;performance...</a><p>The techniques mentioned are stupid fast to the fewest milliseconds, but most JavaScript developers find this incredibly unpopular.
K0IN大约 1 年前
you can &quot;force&quot; integer arithmetic with bit or (example your_float|0), this helps the JavaScript runtime to optimise calls and use (faster) int arithmetic. (<a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;23436159&#x2F;how-bitwise-operations-boost-performance-in-asm-js" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;23436159&#x2F;how-bitwise-ope...</a>)<p>always remember the fastest code is the code that is not executed at runtime.
wryoak大约 1 年前
Don’t use the strict equality operator if type inequality should short circuit to false. It is 30-70% faster to check for equality of the result of typeof, and only when true to check value equality.<p>This makes no sense to me, since checking type is supposedly the first step of each equality operator’s algorithm, but this is the result JSBench keeps returning me.
EchoChamberMan大约 1 年前
I haven&#x27;t used them much, but I would check out typed arrays:<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;TypedArray" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
Cicero22大约 1 年前
if you&#x27;re just trying to initialize a new Array, you can just hardcode it and get much better performance. <a href="https:&#x2F;&#x2F;www.measurethat.net&#x2F;Benchmarks&#x2F;Show&#x2F;29982&#x2F;0&#x2F;hardcoded-array-vs-arrayfrom-vs-new-array-vs-push" rel="nofollow">https:&#x2F;&#x2F;www.measurethat.net&#x2F;Benchmarks&#x2F;Show&#x2F;29982&#x2F;0&#x2F;hardcode...</a><p>This isn&#x27;t necesarrily unknown, but knowing that there&#x27;s a cost associated with promises and async&#x2F;await will get you pretty far in terms of performance. I remember the idea first clicking when I watched this : <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=SMBvjmeOotA" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=SMBvjmeOotA</a>
评论 #39586080 未加载
lotrez大约 1 年前
Typed arrays are fast, for(let i = O; i &lt; max; i++) loops are fast. But also, what kind of tool are you making where you need every milliseconds but also need to use JS?
quickthrower2大约 1 年前
Binge on <a href="https:&#x2F;&#x2F;v8.dev&#x2F;blog" rel="nofollow">https:&#x2F;&#x2F;v8.dev&#x2F;blog</a><p>Use chrome profiler