用document.createElement创建的元素是否保留在内存中? [英] Do elements created with document.createElement stay in memory?

查看:399
本文介绍了用document.createElement创建的元素是否保留在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在慢慢进行chrome扩展,并且我需要解析一些包含html实体的数据,并且需要对其进行解码.我在这里的答案中看到可以使用document.createElement,所以我这样做了:

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;
}

但是我担心document.createElement会留下元素,因为此函数在后台脚本上运行,所以它不像经常刷新一样,并且每5分钟运行35000次.

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.

那么,由document.createElement创建的元素会被释放还是保留? 我的意思是,我没有将它们附加到任何地方,而是将它们附加到局部变量,但是我不确定.

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.

推荐答案

它们将被垃圾回收.特别是,由于您正在开发Chrome扩展程序,因此V8往往会非常快速地回收此类临时文件,因此不必担心.

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.

如果您通常对此感到担心,一种常见的解决方案是只保留一个div来完成这项工作.

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

这篇关于用document.createElement创建的元素是否保留在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆