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.
- 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
1 Answer
Reset to default 8They 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.