简体   繁体   English

有关唯一ID的数据结构最佳实践

[英]Data structure best practice regarding unique id's

I have these two data structures that I continually find myself choosing between when pulling data from a database: 我有这两个数据结构,我不断发现自己在从数据库中提取数据时选择:

{
    "1": {"location": "seattle", "color": "red"},
    "2": {"location": "irvine", "color": "blue"},
    "3": {"location": "san diego", "color": "green"}
}

{
    "listings":[
        {"id": "1", "location": "seattle", "color": "red"},
        {"id": "2", "location": "irvine", "color": "blue"},
        {"id": "3", "location": "san diego", "color": "green"}
    ]
}

Each seems to have pros and cons... 每个人似乎都有利有弊......

The object structure is great for quickly accessing a value given an id by saying obj['3'].color the problem is when listing all objects you have to loop with a for(key in obj) loop which seems to be very slow. 对象结构非常适合通过说obj['3'].color来快速访问给定id的值obj['3'].color问题是当列出所有对象时,你需要使用for(key in obj)循环来循环,这似乎非常慢。

The array structure loops much faster using for(var i=0; i<array.length; i++) but accessing a value given an id is not easy out of the box. 使用for(var i=0; i<array.length; i++) ,数组结构的循环速度要快得多for(var i=0; i<array.length; i++)但是访问给定id的值并不容易开箱即用。 You have to make a function that loops through the entire array checking the id against a supplied parameter. 你必须创建一个循环遍历整个数组的函数,根据提供的参数检查id。

Here's a jsperf of the two solutions. 这是两个解决方案的jsperf

Which do you think is better and why? 您认为哪个更好,为什么?

As always, the answer is: It depends . 一如既往,答案是: 这取决于 Will you mostly access the objects randomly by their id ? 你会主要通过他们的id随机访问这些对象吗? Then use the object. 然后使用该对象。 Will you mostly loop through them in order? 你会主要按顺序循环它们吗? Then use an array. 然后使用数组。

Of course, you can do both, by returning an array and then creating an index on it by id value. 当然,你可以通过返回一个数组,然后通过id值创建一个索引来做到这id Eg, 例如,

var arr = /*...wherever you get your second example, the array...*/;
var index, len, entry;
arr.index = {};
for (index = 0, len = arr.length; index < len; ++index) {
    entry = arr[index];
    arr.index[entry.id] = entry;
}

(I have a function that does this because I find it a useful technique periodically.) (我有一个这样做的功能,因为我发现它是一种有用的技术。)

Now you can loop through them with a loop, or randomly access them via arr.index["some_id"] . 现在你可以用循环遍历它们,或者通过arr.index["some_id"]随机访问它们。 Note that you have to be careful when modifying it (eg, ensure you do deletions and additions in both places). 请注意,修改它时必须小心(例如,确保在两个位置都进行删除和添加)。

Note that there I've used a property on the actual array, called index (the name can be whatever you like; I frequently use index or byId or similar). 请注意,我在实际数组上使用了一个名为index的属性(名称可以是您喜欢的名称;我经常使用indexbyId或类似名称)。 Some people don't like to use non-index properties on arrays; 有些人不喜欢在数组上使用非索引属性; I have no problem with it, since arrays are really just objects anyway . 我没有问题,因为数组实际上只是对象 But you don't have to, you can keep track of the index in its own variable as a peer of arr . 但是你没必要,你可以在自己的变量中跟踪索引作为arr的对等。

Also note that while there may be an absolute difference in the speed of iteration using for..in on the object vs. for on the array, the odds of there being any real-world impact on that speed of iteration is very low. 还要注意的是,虽然有可能在迭代的速度使用绝对for..in的对象与for阵列上,也正对迭代的速度是任何真正的世界影响的几率是非常低的。

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

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