Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete property vs creating new object

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?

like image 430
Tikkes Avatar asked Oct 16 '25 04:10

Tikkes


1 Answers

Neither 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.

like image 110
T.J. Crowder Avatar answered Oct 17 '25 18:10

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!