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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Some features that every JavaScript developer should know in 2025

19 点作者 pierremenard大约 1 个月前

2 条评论

bhaney大约 1 个月前
I wish authors would actually test their performance claims before publishing them.<p>I quickly benchmarked the two code snippets:<p><pre><code> arr.slice(10, 20).filter(el =&gt; el &lt; 10).map(el =&gt; el + 5)</code></pre> and<p><pre><code> arr.values().drop(10).take(10).filter(el =&gt; el &lt; 10).map(el =&gt; el + 5).toArray()</code></pre> but scaled up to much larger arrays. On V8, both allocated almost exactly the same amount of memory, but the latter snippet took 3-4x as long to run.
评论 #43689807 未加载
评论 #43692556 未加载
gnabgib大约 1 个月前
<i>Iterator Helpers:</i><p><pre><code> arr.slice(10, 20).filter(el =&gt; el &lt; 10).map(el =&gt; el + 5) </code></pre> &gt; This is really inefficient because for each transformation a new array should be allocated.<p>.slice[0] does not allocate, nor does .filter[1], only map does.. so one allocation.<p><pre><code> arr.values().drop(10).take(10).filter(el =&gt; el &lt; 10).map(el =&gt; el + 5).toArray() </code></pre> Allocates once for .values[2], and again for .toArray[3].. there&#x27;s decreased efficiency here.<p><i>Swapping variables</i>:<p>Only do this if you don&#x27;t care about performance (the advice is written like using the array swap hack is categorically better).<p>[0]: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Array&#x2F;slice" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>[1]: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Array&#x2F;filter" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>[2]: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Set&#x2F;values" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>[3]: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Iterator&#x2F;toArray" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
评论 #43688659 未加载
评论 #43689458 未加载
评论 #43689662 未加载