问题
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