I am processing a heavy array of objects on a node app, and at some point I want to remove two properties from all objects in the array.
At the same time I am measuring the node memory impact on rss (Residente Set Size).
What I am finding is that just the act of deleting them consumes a lot of memory.
Example, it's actually a lot bigger and with lots of objects. File size of json is 200MB.
[
{
keep: 'foo',
prop1: 'remove',
prop2: 'remove'
},...
]
This consumes the most from 500MB goes to 1000MB
const clean = original.map((obj) => {
delete obj.prop1
delete obj.prop2
return obj
})
This also consumes a lot, also around 1000MB
original.forEach((obj) => {
delete obj.prop1
delete obj.prop2
})
This comsumes the least, goes to around 650MB
const clean = original.map(({ prop1, prop2, ...obj }) => obj)
But if I do not delete them at all then it does not consume anymore than the original 500MB. What is going on? Should not removing properties make the memory lighter?
mapcreates a new array. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… if you want to loop useforEach.map()method produces an array of the same size so that should be fairly straight forward. The.foreach()method passes the entire array as a parameter - hence doubles up for that reason..map()too, and it also doubles up