简体   繁体   English

JS对象数组与JS对象数组的效率和性能

[英]JS Object of Arrays vs JS Array of Objects efficient and performance

This question regarding javascript language. 这个关于javascript语言的问题。 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. (如果我做错了,请纠正,我想尽可能地简化问题,这就是为什么)谢谢。

Just for the sake of curiosity I went ahead and used console.time() to benchmark the results. 出于好奇,我继续使用console.time()对结果进行基准测试。

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. 您为每个计时器指定一个唯一的名称,并且在给定页面上最多可以运行10,000个计时器。 When you call console.timeEnd() with the same name, the browser will output the time, in milliseconds, that elapsed since the timer was started. 当您使用相同的名称调用console.timeEnd()时,浏览器将输出自计时器启动以来经过的时间(以毫秒为单位)。

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. 这些是我的计算机上进行1000000次操作的结果*。

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. 正如@Nina Scholz非常正确地提到的那样,选择使您的生活更加轻松的数据结构。

[*] 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 . 在线尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM