(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43688460

Hacker News 上的一个帖子讨论了一篇关于 2025 年 JavaScript 核心功能的博客文章。评论者 bhaney 对作者关于迭代器辅助函数的性能说法提出了质疑,他发现 `arr.values().drop().take().filter().map().toArray()` 比传统的 `arr.slice().filter().map()` 慢得多,而且内存分配也差不多。 Gnabgib 进一步解释说,迭代器辅助函数会多次分配内存(.values 和 .toArray),降低了效率。他们还批评了使用数组交换技巧来提高性能的建议。 Golergka 认为,大多数 JS/TS 代码优先考虑可读性而不是性能,但是关键的性能部分需要不同的编码风格。 Gnabgib 回复说,他们不同意新方法更易读,并继续指出 C# Linq 的性能损失几乎可以忽略不计,因为 C# 引擎进行了优化。但 JavaScript 还达不到这个水平,多个 JavaScript 引擎都需要赶上来。此外,迭代器辅助函数链中的单个函数无法预测后续步骤以提高效率,而优秀的软件工程师可以做到这一点。


原文
Hacker News new | past | comments | ask | show | jobs | submit login
Some features that every JavaScript developer should know in 2025 (waspdev.com)
12 points by pierremenard 1 hour ago | hide | past | favorite | 4 comments










I wish authors would actually test their performance claims before publishing them.

I quickly benchmarked the two code snippets:

    arr.slice(10, 20).filter(el => el < 10).map(el => el + 5)
and

    arr.values().drop(10).take(10).filter(el => el < 10).map(el => el + 5).toArray()
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.


Iterator Helpers:

  arr.slice(10, 20).filter(el => el < 10).map(el => el + 5)
> This is really inefficient because for each transformation a new array should be allocated.

.slice[0] does not allocate, nor does .filter[1], only map does.. so one allocation.

  arr.values().drop(10).take(10).filter(el => el < 10).map(el => el + 5).toArray()
Allocates once for .values[2], and again for .toArray[3].. there's decreased efficiency here.

Swapping variables:

Only do this if you don't care about performance (the advice is written like using the array swap hack is categorically better).

[0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...



Vast majority of JS/TS we write doesn’t live on the critical path and should be optimised for readability over performance.

However, when it does, it should be explicitly recognised, appropriate boundaries put, and that code written in a completely different style altogether. There are A LOT more of idiomatic and widespread JS/TS patterns that should not be used in such code.

Before TS, I used to write games in C# (Unity), and it was the same there too. I think this distinction between two kinds of code holds in a lot of environments.



Neither of these cases are more readable, if you want a readable swap can you get better than:

  const [a,b]=swap(b,a);
It does look a lot like linq, and has the same hidden complexity problem of linq.. while C# went through extreme lengths to optimise the performance penalties down to almost negligible - JS isn't there yet (and may never be after requiring multiple engines to catch up). An individual function in a chain of drop/jump/take/filter/change(map)/cast steps cannot look ahead to future needs, nor re-order for better efficiency... a (good) software engineer can. In the same way there's good ways to write SQL (benefit from indexes and reduce the set quickly) and bad (cause full table scans).






Join us for AI Startup School this June 16-17 in San Francisco!


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



Search:
联系我们 contact @ memedata.com