【发布时间】:2020-11-25 07:47:29
【问题描述】:
我的问题类似于Do local variables inside of a loop get garbage collected?
我有一个类使用递归在打字机风格的角色对话屏幕中播放 blip 音效:
class DialogueBox extends Component{
constructor(props){
super(props)
this.state = {...
}
...
}
typeWriter(txt,i,speed=50) {
if(i==txt.length){
...
return
}
else{
// a blip sound effect plays on every new character typed
let sfx = new Audio(speechBlip);
sfx.play();
...
setTimeout(()=>this.typeWriter(txt,i+1,speed),speed);
}
}
注意被多次实例化的局部变量let sfx = new Audio(speechBlip)。这会导致内存中存储的大量音频对象永远不会被清理吗?
我之所以使用这种方法,是因为我喜欢它的声音,而不是在构造函数中创建一个 Audio() 并将其重新设置为 0 时间或仅在文件完成播放时重放。
这种方法会严重拖累记忆吗?我尝试使用开发工具的内存面板,但我不确定我是否正确解释它并且不确定它将如何扩展......
【问题讨论】:
标签: javascript performance memory