Do elements created with document.createElement stay in memory?

穿精又带淫゛_ 提交于 2020-02-01 18:34:35

问题


Hi, I'm slowly making a chrome extension, and I need to parse some data that contains html entities, and I need to decode it. I saw in an answer here that I could use document.createElement for it, so I did this:

htmlDecode: function(input) {
    if(/[<>]/.test(input)) { // To avoid creating tags like <script> :s
        return "Invalid Input";
    }
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

However I'm worried that document.createElement leaves elements behind because this function runs on the background script, so it's not like it gets refreshed often, and it runs around 35000 times every 5 minutes.

So, do elements created by document.createElement get freed, or do they stay? I mean, I do not append them anywhere and they are assiged to a local variable, but I'm not sure.


回答1:


They will be garbage collected. In particular, since you're developing a Chrome extension, V8 tends to recycle temporaries like this very quickly so it shouldn't be much of a concern.

If you are worried about this in general, one common solution is to simply keep a single div around to do the job.



来源:https://stackoverflow.com/questions/15320853/do-elements-created-with-document-createelement-stay-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!