简体   繁体   English

Javascript垃圾回收在多大程度上影响性能?

[英]How much does Javascript garbage collection affect performance?

I'm writing a bunch of scripts that present images serially (eg 1 per second) and require the user to make either a keyboard or mouse response. 我正在编写一堆脚本,这些脚本可以顺序显示图像(例如,每秒1张),并要求用户做出键盘或鼠标响应。

I'm using closures to handle the timing of image presentation and user input. 我正在使用闭包来处理图像呈现和用户输入的时间。 This causes garbage collection to happen pretty frequently and I'm wondering if that will affect the performance (viz. timing of image presentation). 这会导致垃圾收集非常频繁地发生,我想知道这是否会影响性能(即图像呈现的时间)。

Depends on how the closures work and the images. 依赖于闭包是如何工作和图像。 For instance a system which has about 5 different images but 20 different kinds of input would run more efficient if the closures know about the image but take the user input as argument, rather than vice versa -- because you can make do with a maximum of 5 of those closures at any given time as opposed to about 20. 例如,如果闭包知道图像,但将用户输入作为参数,而不是反之,则具有大约5个不同图像但20种不同输入的系统将更有效地运行-因为您可以最大程度地在任何给定时间关闭5个,而不是大约20个。

Also you might be able to convert this into a system that works like: 另外,您也许可以将其转换为如下系统:

  1. Image display object a single instance of which can be re-used over multiple images (it should contain only the most limited amount of state). 图像显示对象的单个实例可以在多个图像上重复使用(它应该只包含最有限的状态)。
  2. User input functions/objects which trigger events. 触发事件的用户输入功能/对象。
  3. Event handler for user input which reconfigures (1) depending on the things emited by (2). 用户输入的事件处理程序,根据(2)发出的内容重新配置(1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM