最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - delete property vs creating new object - Stack Overflow

programmeradmin8浏览0评论

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?

Share Improve this question asked Jul 26, 2016 at 8:27 TikkesTikkes 4,6894 gold badges38 silver badges66 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 9

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.

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 )

发布评论

评论列表(0)

  1. 暂无评论