Based on this answer, this will "load all the keys into memory":
Object.keys(o).forEach(function(key) {
var val = o[key];
logic();
});
Is the memory "freed" when this is done? Or does something need to be "nulled", or the like?
Based on this answer, this will "load all the keys into memory":
Object.keys(o).forEach(function(key) {
var val = o[key];
logic();
});
Is the memory "freed" when this is done? Or does something need to be "nulled", or the like?
Yes, the memory will be freed implicitly after the forEach
has completed, as afterwards nothing on the stack will reference the array created by Object.keys
any more. (There is no variable to nullify, btw)
You could've written equivalently
{
const keys = Object.keys(obj);
keys.forEach(function(key) {
const val = o[key];
logic();
});
}
where it is a bit more obvious that the whole keys
array is hanging around during the iteration (in contrast to a "lazy" for in
enumeration), and also that it goes out of scope immediately after (when the block ends).