Memory leak in JavaScript (Chrome)

前端 未结 2 1539
情话喂你
情话喂你 2021-02-03 12:34

I\'m calling a function 50 times a second, which does some expensive things as it is painting alot on a element.

It works great, no problems

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-03 13:33

    I'm just going to pull this quote directly, linked from the article;

    Speaking of memory leaks, breaking circular references — the cause of the leaks — is usually done with simple null assignment. There’s usually no need to use delete. Moreover, null‘ing allows to “dereference” variables — what delete would normally not be able to do.

    var el = document.getElementById('foo');
    // circular reference is formed
    el.onclick = function() { /* ... */ };
    // circular reference is broken
    el = null;
    // can't `delete el` in this case, as `el` has DontDelete
    

    For these reasons, it’s best to stick with null‘ing when breaking circular references.

    delete Explained

提交回复
热议问题