简体   繁体   English

Javascript对象与数组性能

[英]Javascript object versus array performance

I want to save data and have quick access to it. 我想保存数据并可以快速访问它。 All of my data has a unique ID. 我所有的数据都有唯一的ID。

I can think of two possibilities. 我可以想到两种可能性。

object: 宾语:

myObject[id] = myDataObject;

array: 数组:

myArray.push(myDataObject);

In the case of an array the ID would be inherited inside the myDataObject. 如果是数组,则ID将在myDataObject内部继承。 If I want to search for some data now according to the ID, how would I do this. 如果我现在想根据ID搜索一些数据,该怎么做。 What would be the quickest? 最快的是什么?

What would be faster respecting following situation: 什么会更快地尊重以下情况:

  1. the array is sorted by the ID in the myDataObject everytime a new ID is "pushed" 每次“推”新ID时,数组均按myDataObject中的ID排序
  2. searching the Array for the needed data is done by some kind of quicksort algorithm 通过某种快速排序算法在数组中搜索所需数据

How does the object handle this: 对象如何处理:

var someCrazyID = 1233AFE12B00ED;
console.log(myObject[someCrazyID].attribute);

Does it itterate ober the whole object, or does it do something simular in the background as I would do with the array manually? 它会使整个对象烦恼,还是像我手动处理数组那样在后台做类似的事情?

Please argument only in the aspect of performance first of all and in the end I would also like to know your general oppinion (maintenance etc.) 首先请仅在性能方面争论,最后我也想知道您的总体看法(维护等)

Thanks for your help in advance 谢谢您的帮助

In javascript, objects are basically hash-maps, meaning that looking up a certain value given a key is a constant O(1) cost operation. 在javascript中,对象基本上是哈希映射,这意味着在给定键的情况下查找某个值是常量O(1)成本操作。 That makes objects ideal for lookup-based functionality, so you probably want to go with objects, so there will be no need for sorting (of course, objects by definition are an unsorted collection of their key-value pairs), and the lookup will be fast regardless. 这使对象成为基于查找的功能的理想选择,因此您可能需要使用对象,因此无需排序(当然,根据定义,对象是键值对的未排序集合),查找将无论如何都要快。

Do note though that in JS arrays are also objects, with the same lookup properties, the only difference is that they follow the convention of using subsequent integers for keys. 请注意,尽管在JS数组中对象也是具有相同查找属性的对象,唯一的区别是它们遵循对键使用后续整数的约定。 If your IDs are integers, you can also create a sort of sparse array by using the IDs as indexes, but that's basically the same as creating an object with the IDs as keys. 如果您的ID是整数,则还可以使用ID作为索引来创建一种稀疏数组,但这与以ID为键创建对象基本上相同。

Depends on how many objects you wish to store. 取决于您要存储多少个对象。 If there aren't many objects then access by object property is just fine. 如果对象不多,则按对象属性访问就可以了。 However, with increasing amount of data arrays will be a way faster. 但是,随着数据阵列数量的增加,速度会更快。

Let's test it and perform an experiment. 让我们对其进行测试并进行实验。

Repeat access to object property and array for 1 million times and measure times. 重复访问对象属性和数组一百万次并进行测量。

var obj = new Object(),
    arr = [];

// init
// let's give our object 1 million properties and to array 1 million elements
for (var i=0; i<1000000; i++) {
    obj[i] = i; //instead of obj["prop"+(i)]
    arr.push(i);
};

// let's repeat access to object property 1 million times
var d1 = (new Date()).getTime();

var x;
for (var i=0; i<1000000; i++) {
   x = obj[i]; //instead of obj["prop"+(i)]
}

var ms1 = (new Date()).getTime() - d1;
alert("Access to object property took " + ms1 + "ms.");

// now let's repeat access to array 1 million times
var d2 = (new Date()).getTime();

var y;
for (var i=0; i<1000000; i++) {
   y = arr[i];
}

var ms2 = (new Date()).getTime() - d2;
alert("Access to array took " + ms2 + "ms.");

Here is JSFiddle: http://jsfiddle.net/0d7qu0hd/1/ 这是JSFiddle: http : //jsfiddle.net/0d7qu0hd/1/

The results on my laptop are clear: 我的笔记本电脑上的结果很清楚:

Accessing object member: 500ms on average 访问对象成员: 平均500ms

Accessing array by index: 3ms on average 按索引访问数组: 平均3ms

EDIT: As Max pointed out - the conversion really takes additional time. 编辑:正如马克斯指出-转换确实需要额外的时间。 Without a conversion the results are the same. 没有转换,结果是相同的。

EDIT 2: After performing several tests it seems that accessing obj[i] is slightly faster than array[i]. 编辑2:在执行几次测试后,似乎访问obj [i]的速度比array [i]略快。 2ms vs. 3-4 ms. 2毫秒-3-4毫秒。

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

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