这是 fiddle :http://jsfiddle.net/36mdt/
大约 10-20 秒后,显示屏在崩溃后不久开始随机卡住。我无法在 Firefox 中重现这一点。
分析显示没有异常。
http://jsfiddle.net/3pbdQ/显示肯定存在内存泄漏。即使是 1 FPS,内存使用量也会达到每帧 5 兆字节。
顺便说一句,这个例子确实展示了 Math.random() 并不是那么随机。
最佳答案
我只做了 2 项性能改进,5 分钟后它没有崩溃(似乎也没有泄漏内存)。结帐http://jsfiddle.net/3pbdQ/3/
- 不在每次迭代中计算大小
- 使用超时而不是卡住间隔。
- 使用位运算符对数字取底
Profiling reveals nothing unusual.
Chrome Profiler 不适用于 WebWorkers,据我所知。根据与 Paul Irish 的对话:
"Check about:inspect for shared workers, also you can do console.profile() within the worker code (I THINK) and capture those bits. The "cleans up" is the garbage collector: if after the cleanup there is still a growing line of excess memory, then thats a leak."
和
On a side note, this example really shows how Math.random() is really not so random.
众所周知,没有完美的随机算法,但无论如何,您看到的一堆分组颜色是因为您没有设置 canvas.height 和 canvas.width,它与 CSS 值不同。
编辑:仍在泄漏内存,我不知道为什么,大约 10 秒后它“清理”了。超出我的知识范围,但在 60 FPS 下运行流畅(var TIME = 16
)
关于JavaScript 在 Chrome 中卡住/崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036765/