JS Object of Arrays vs JS Array of Objects efficient and performance

Viewed 261

This question regarding javascript language. Simply think we have a map and we insert item as following manner

var dataMap=new Map();

//First Mechanism
//firstly we can think of structure of values of map can be JSONArray of objects
 dataMap.set("key1",[{'id':12,'name':"obj1"}]); // init
// and,then insert  new element to JSON Array which holds by map using 'key1'
dataMap.get("key1").push({'id':23,'name':"obj47"});//updated, now value of 'key1' is an JSON array which holds two elements
// expect 'key1' ->   [{'id':12,'name':"obj1"},{'id':23,'name':"obj47"}]          

//Second mechanism
// next we cant think of structure of values of map as JSONObject of Arrays
dataMap.set("key1",{'id':[12],'name':["obj1"]}); // init
// then we proceed with update operations like this
dataMap.get("key1").id.push(23);
dataMap.get("key1").name.push("obj47"); // two operations to insert items to respective arrays.
// expect 'key1' ->{'id':[12,23],'name':["obj1","obj47"]}

Which approach is most effective and efficient?

Think we have considerable amount of insertion operations to map ,if we are in to performance which one is better?

(If I have done mistake please correct ,I wanted to simplify the question as possible as I can that's why) Thank you.

1 Answers

Just for the sake of curiosity I went ahead and used console.time() to benchmark the results.

Starts a timer you can use to track how long an operation takes. You give each timer a unique name, and may have up to 10,000 timers running on a given page. When you call console.timeEnd() with the same name, the browser will output the time, in milliseconds, that elapsed since the timer was started.

Now you can argue about how reliable its results are considering there are other factors involved such as browser caching etc.

These are the results* for 1000000 operations on my machine.

Chrome Version 61.0.3163.91 (Official Build) (64-bit)

// 1st run
default: 2217.048095703125ms
default: 3032.159912109375ms
// 2nd run
default: 1948.16796875ms
default: 3320.7431640625ms
// 3rd run
default: 2177.461181640625ms
default: 2989.448974609375ms


Firefox 55.0.3 (32-bit)

// 1st run
default: 2146.64ms
default: 2390.11ms
// 2nd run
default: 1863.7ms
default: 2264.02ms
// 3rd run
default: 1751.7ms
default: 2283.6ms

You can see that the difference is not that big and should not be a factor affecting your decision. As @Nina Scholz very correctly mentioned choose the data structure that will make your life easier.

[*] Code used in the benchmark as reference:

let dataMap = new Map();
const diff = 1000000;
let key = null;

console.time();
for(let i = 0; i < 1000000; i++){
    key = `key${i}`;
    dataMap.set(key, [{'id': i, 'name': `obj${i}`}]);
    dataMap.get(key).push({'id': i + diff, 'name': `obj${i + diff}`})
}   
console.timeEnd();

dataMap = new Map();

console.time();
for(let i = 0; i < 1000000; i++){
    key = `key${i}`;
    dataMap.set(key, {'id':[i], 'name': [`obj${i}`]});
    dataMap.get(key).id.push(i + diff);
    dataMap.get(key).name.push(`obj${i + diff}`);
}
console.timeEnd();

Or try it online.

Related