So I have an object as follows:
var data = [
{
unmatchedLines: [], //possible big array ~700+ lines per entry
statusCode: 200,
title: "title",
message: "some message",
details: [],
objectDetails: []
},
{
//same object here
}
];
This array gets filled by service calls and then looped through for merging the output before sending this result back to the frontend.
function convertResultToOneCsvOutput(data) {
var outPutObject = {
unmatchedLines: [],
responses: []
};
for(var i = 0; i < data.length; i++) {
if(data[i].fileData) {
outPutObject.unmatchedLines = outPutObject.unmatchedLines.concat(data[i].fileData);
}
outPutObject.responses.push({
statusCode: data[i].statusCode,
title: data[i].title,
message: data[i].message,
details: data[i].details,
objectDetails: data[i].objectDetails,
})
}
return outPutObject;
};
Now I was first using delete
to get rid of the unmatchedLines
from the data
object so that, in stead of creating a whole new object and pushing this to the output I could do:
delete data[i].unmatchedLines;
outPutObject.responses.push(data[i]);
But then I read that delete
is very slow and stumbled upon a post stating that putting this to undefined
in stead of using delete
would be faster.
My question:
What is better to use in the end? delete
, setting to undefined
or creating a new object in itself like I am doing right now?
So I have an object as follows:
var data = [
{
unmatchedLines: [], //possible big array ~700+ lines per entry
statusCode: 200,
title: "title",
message: "some message",
details: [],
objectDetails: []
},
{
//same object here
}
];
This array gets filled by service calls and then looped through for merging the output before sending this result back to the frontend.
function convertResultToOneCsvOutput(data) {
var outPutObject = {
unmatchedLines: [],
responses: []
};
for(var i = 0; i < data.length; i++) {
if(data[i].fileData) {
outPutObject.unmatchedLines = outPutObject.unmatchedLines.concat(data[i].fileData);
}
outPutObject.responses.push({
statusCode: data[i].statusCode,
title: data[i].title,
message: data[i].message,
details: data[i].details,
objectDetails: data[i].objectDetails,
})
}
return outPutObject;
};
Now I was first using delete
to get rid of the unmatchedLines
from the data
object so that, in stead of creating a whole new object and pushing this to the output I could do:
delete data[i].unmatchedLines;
outPutObject.responses.push(data[i]);
But then I read that delete
is very slow and stumbled upon a post stating that putting this to undefined
in stead of using delete
would be faster.
My question:
What is better to use in the end? delete
, setting to undefined
or creating a new object in itself like I am doing right now?
- 1 You could read this: smashingmagazine./2012/11/… – Ron van der Heijden Commented Jul 26, 2016 at 8:37
- @RonvanderHeijden Nice, thank you for this. – Tikkes Commented Jul 26, 2016 at 8:38
2 Answers
Reset to default 9Neither is "better."
It is true that when you delete
a property from an object, on many modern engines that puts the object into a slower "dictionary mode" than it would be if you didn't delete the property from it, meaning that subsequent property lookups on that object (data[i]
in your case) will be slower than they were before the delete
. So setting the property to undefined
instead (which is not quite the same thing) may be appropriate in those very rare situations where the speed of property access on the object matters.
It could be true, but it really depends on the length of the list, however:
any performance difference between the two techniques is going to be immeasurably small in any typical case.
( more info about delete )
Anyway: if you really need extra performance, you should consider dealing with clones and an immutable data-set ( more info about immutable performances )