Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do elements created with document.createElement stay in memory?

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.

like image 715
Goodwine Avatar asked Mar 10 '13 09:03

Goodwine


People also ask

What is this document createelementns after all?

So, what is this document.createElementNS after all? Most of us may be more familiar with document.createElement, which we used to create a new tag node. Accordingly, document.createElementNS does much the same thing, only that it takes an extra namespace URI as parameter.

What is the use of createelement method?

Definition and Usage. The createElement() method creates an Element Node with the specified name. Tip: After the element is created, use the element.appendChild() or element.insertBefore() method to insert it to the document.

What is the difference between document createelement () and element appendChild ()?

The document.createElement () creates a new HTML element. The element.appendChild () appends an HTML element to an existing element. Was this tutorial helpful ?

How do I create a new element in a DOM document?

DOMDocument::createElement — Create new element node. public DOMDocument::createElement ( string $name string $value ] ) : DOMElement. This function creates a new instance of class DOMElement. This node will not show up in the document unless it is inserted with (e.g.) DOMNode::appendChild().


1 Answers

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.

like image 173
Justin Summerlin Avatar answered Oct 26 '22 20:10

Justin Summerlin