简体   繁体   English

如何在O(1)的redux / vuex存储中更新元素?

[英]How to update an element in a redux/vuex store in O(1)?

How do you usually update an element(hash) in a list in the store (redux, vuex) in O(1)? 您通常如何在O(1)的商店(redux,vuex)的列表中更新元素(哈希)?

I need the order of the elements, it's important (I can add/remove elements from the list). 我需要元素的顺序,这很重要(我可以从列表中添加/删除元素)。 I will update elements on each millisecond, so a lot of updates will be made. 我将每毫秒更新一次元素,因此将进行很多更新。

My question is, it's very bad to save the index of the element as a property of the element? 我的问题是,将元素的索引另存为元素的属性非常不好? Or if the list has 500-1000 elements, it's ok to use .find by Id for example? 或者,如果列表中包含500-1000个元素,例如可以使用ID查找.find吗?

Normalize your store shape with two properties 通过两个属性规范化商店形状

  • Map of id to entity ID到实体的映射
  • List of ids in the required order. ID列表(按所需顺序)。

    { byId : { "post1" : { id : "post1", author : "user1", body : "......", comments : ["comment1", "comment2"] }, "post2" : { id : "post2", author : "user2", body : "......", comments : ["comment3", "comment4", "comment5"] } }, allIds : ["post1", "post2"] }

With this structure, you can update a particular entity in O(1) complexity, and also retrieve the entities in the order of insertion by iterating allIds . 使用这种结构,您可以更新O(1)复杂性的特定实体,还可以通过迭代allIds按插入顺序检索实体。

More information can be found here 更多信息可以在这里找到

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

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