最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do elements created with document.createElement stay in memory? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Mar 10, 2013 at 9:40 GoodwineGoodwine 1,7181 gold badge19 silver badges31 bronze badges 1
  • 2 Sure, nothing references the div anymore after the function is ran, so it will be eventually garbage-collected. – Bergi Commented Mar 10, 2013 at 11:33
Add a ment  | 

1 Answer 1

Reset to default 8

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 mon solution is to simply keep a single div around to do the job.

发布评论

评论列表(0)

  1. 暂无评论