foo.v = {
rolo: 'a',
cholo: 'b',
yolo: 'c'
};
I have a long-running stateful process and I create new objects for certain types of requests. My question is, if I have an object like v in memory.
Is it more performant in terms of memory re-allocation to do:
delete foo.v.cholo;
or is it sometimes better to do:
foo.v.cholo = undefined
I read that inside the bowels of V8, that the delete operator mutates objects and prevents them from being as easily garbage collected or something like that. See:
https://github.com/google/google-api-nodejs-client/issues/375
I also would consider using a Map instead of an object, I tried comparing the delete operator to Map.prototype.delete, but the the delete operator was more performant here, maybe my test is not very good:
{
const obj = {};
const start = Date.now();
for(let i =0; i < 1000000; i++){
obj['foo'] = true;
delete obj['foo'];
}
console.log('done after:', Date.now() - start);
}
{
const m = new Map();
const start = Date.now();
for(let i =0; i < 1000000; i++){
m.set('foo',true);
m.delete('foo');
}
console.log('done after:', Date.now() - start);
}