我在 Stack Overflow 上发现了一些关于内存使用和 CPU 使用的有趣帖子,但是他们都没有直接解决这个看似简单的问题:
As a generic strategy in a JavaScript app, is it better in terms of performances to use memory (storing data) or CPU (recalculating data each time)?
我指的是常见浏览器环境(FF、Chrome、IE>8)中的javascript使用
有人对此有更直接且有记录的答案吗?
--- 编辑 ---
好的,我知道这个问题很笼统。我尝试缩小“范围”。
阅读您的答案后,我意识到真正的问题是:“如何理解我的 javascript 代码仍然具有良好性能的内存限制?”。 环境:常用浏览器环境(FF、Chrome、IE>8)
我使用的函数不是非常复杂的数学函数,但可以产生相当大量的数据(300-400kb),我想了解是否每次都重新计算它们或将结果存储在变量中更好。
最佳答案
模糊相关 - 当您开始使用大型对象/数组时,浏览器中的 JS 非常消耗内存。如果您考虑由 Canvas 元素或其他富媒体 API 生成的二进制数据,那么显然您不希望以传统方式存储这些数据 - 忽略性能问题,这也很重要。
来自MDN article talking about JS Typed Arrays :
As web applications become more and more powerful, adding features such as audio and video manipulation, access to raw data using WebSockets, and so forth, it has become clear that there are times when it would be helpful for JavaScript code to be able to quickly and easily manipulate raw binary data.
这是一个JS Perf comparison of arrays ,还有另一个looking at canvas in particular ,这样您就可以获得一些有关它们如何工作的直接示例。希望这有用。
关于javascript - CPU 与内存使用情况(理论),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9903756/