Everything works as expected.
OP code adds elements to the DOM, which uses a bunch of C ++ memory. Then, when Javascript is attached to these elements, a wrapper object is created that uses Javascript memory. And this memory usage appears in Chrome's memory profilers .
Interestingly, if you add a name attribute for each new div, then there is an immediate memory burst of 0.5 MB. Just adding the identifier alone (for example, OP code) does not create such a splash (with div). This can be tested using the code snippet below and the Chrome profiler.
Here is a previous SO question that might explain this better:
Do DOM tree elements with identifiers become global variables?
Test code
var LIMIT = 10000, zone = document.getElementById('zone'), count = document.getElementById('count'); window.b1.onclick = function() { var i; for (i = 0; i < LIMIT; i++) { zone.appendChild(document.createElement('div')); } show(); } window.b2.onclick = function() { var i, e; for (i = 0; i < LIMIT; i++) { e = document.createElement('div'); e.id = 'id' + i; zone.appendChild(e); } show(); } window.b3.onclick = function() { var i, e; for (i = 0; i < LIMIT; i++) { e = document.createElement('div'); e.name = 'na' + i; zone.appendChild(e); } show(); } window.b4.onclick = function() { var i, e; for (i = 0; i < LIMIT; i++) { e = document.createElement('div'); e.id = 'id' + i; e.name = 'na' + i; zone.appendChild(e); } show(); } window.b5.onclick = function() { zone.innerHTML = ''; show(); } function show( ) { var e = zone.getElementsByTagName('div'); count.innerHTML = 'total elements = ' + (e ? e.length: '0'); }
button { width: 8em; }
<div>Memory Test: <span id="count"></span></div> <button id="b1">none</button> <button id="b2">with id</button> <button id="b3">with name</button> <button id="b4">with name + id</button> <button id="b5">clear</button> <div id="zone"></div>
source share