0

I wanted to know if there is some performance issue related in the two below coding paradigms. Which one is the best and why?

var data = [{
        "name": "ABC",
        "code": 1,
        "age": 97
    },{
        "name": "XYZ",
        "code": 12,
        "age": 12
    },{
        "name": "LMN",
        "code": 121,
        "age": 172
    }
];

var response = [];

Method1

data.forEach(function(entry){
        var obj = {
            "NAME": entry["name"],
            "AGE": entry["age"]
        };
        response.push(obj);
});

Method2

data.forEach(function(entry){
        var obj = {};
        obj["NAME"] = entry["name"];
        obj["AGE"] = entry["age"];
        response.push(obj);
});

For output object, I need say suppose, only 10 keys out of 100 keys The object has many keys in it. Only limited are shown in the example. Which coding standard to choose and why? Please, can anybody explain?

2
  • 1
    All the major JS implementations now do just in time compilation. How well each optimises this depends on that engine. Commented Sep 7, 2018 at 10:14
  • 1
    jsben.ch/ps8CS For me in Chrome the first one is the fastest. Why?, you might need to look at Chrome's source code to work that one out.. :) Commented Sep 7, 2018 at 10:28

2 Answers 2

2

No need to create the objects and then make a Array.prototype.push() for everly loop iteration...

You can directly use Array.prototype.map() instead of Array.prototype.forEach() and better access the object property values by the dot notation:

const data = [{
    "name": "ABC",
    "code": 1,
    "age": 97
  },{
    "name": "XYZ",
    "code": 12,
    "age": 12
  },{
    "name": "LMN",
    "code": 121,
    "age": 172
  }
];

const response = data.map(obj => ({
  NAME: obj.name,
  AGE: obj.age
}));

console.log(response)

Sign up to request clarification or add additional context in comments.

4 Comments

You're not answering the question.
I think @FK82 is not denying that it is a better code. The original question shows two ways of doing the same thing and is asking which is faster, you did not comment on that.
@YosvelQuintero What @rmn said. The question is about the performance difference between two kinds of object creation. If you want to argue that your code is better performance wise, you should provide some justification. Otherwise, this is not an answer to the question.
@FK82 is correct, you didn't really answer the question, but map is the better option, and also in Chrome at least is also faster -> jsben.ch/gSgED
1

Both approaches are fine and one should not be "faster" than the other, at least not enough to justify using the other one. Use the one you like.

But, if you're still looking for closure. The first one should be faster since it defines the entire object all at one go. There's no "look-ups" the engine have to do. It's all done at one go.

On a separate note, consider using the dot notation, it should be faster since the engine will not have to create strings. So, change

entry["name"]

to

entry.name

Also, if this is your actual code and the purpose is to modify the result to have just name and age and no code. You can do

data.forEach(user => delete user.code)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.