规范化性能:allIds 数组与 Object.entries 函数
Normalisation Performance: allIds array vs Object.entries function
在 redux 和其他情况下,我们可能希望规范化 JavaScript 本质上是关系的对象,而不是具有深层嵌套的对象。
例如,redux 显示了一个建议拆分代码的方法:
const byID = {
'1': {
some: 'object'
},
'2': {
some: 'objectAgain'
},
'3': {
some: 'objectAnotherAgain'
},
};
const allIds = ['1', '2', '3'];
但是,持有 allIds 数组与仅调用类似以下内容相比有什么好处:
Object.keys(byId);
将所有对象键作为数组保存更快吗?还是它们具有相似的时间复杂度?将每个键保存在数组中不会是重复代码吗?
Object.keys(byId) returns 与allIds完全相同的数组,但redux的目的是将所有数据存储尽可能扁平 一个对象,对吧?在对象外部调用 Object.keys() 方法而不保存它与此相反...
var array = {
posts: {
byId: {
"post1": {
id: "post1",
author: "user1",
body: "......",
comments: ["comment1", "comment2"]
},
"post2": {
id: "post2",
author: "user2",
body: "......",
comments: ["comment3", "comment4", "comment5"]
}
},
allIds: ["post1", "post2"]
},
comments: {
byId: {
"comment1": {
id: "comment1",
author: "user2",
comment: ".....",
},
"comment2": {
id: "comment2",
author: "user3",
comment: ".....",
},
"comment3": {
id: "comment3",
author: "user3",
comment: ".....",
},
"comment4": {
id: "comment4",
author: "user1",
comment: ".....",
},
"comment5": {
id: "comment5",
author: "user3",
comment: ".....",
},
},
allIds: ["comment1", "comment2", "comment3", "commment4", "comment5"]
},
users: {
byId: {
"user1": {
username: "user1",
name: "User 1",
},
"user2": {
username: "user2",
name: "User 2",
},
"user3": {
username: "user3",
name: "User 3",
}
},
allIds: ["user1", "user2", "user3"]
}
}
var keys = Object.keys(array.users.byId);
console.log(keys); // need to call a function where i should already know what i want
var simpleKeys = array.users.allIds
console.log(simpleKeys); // way faster too
Relevant links:
在 redux 和其他情况下,我们可能希望规范化 JavaScript 本质上是关系的对象,而不是具有深层嵌套的对象。
例如,redux 显示了一个建议拆分代码的方法:
const byID = {
'1': {
some: 'object'
},
'2': {
some: 'objectAgain'
},
'3': {
some: 'objectAnotherAgain'
},
};
const allIds = ['1', '2', '3'];
但是,持有 allIds 数组与仅调用类似以下内容相比有什么好处:
Object.keys(byId);
将所有对象键作为数组保存更快吗?还是它们具有相似的时间复杂度?将每个键保存在数组中不会是重复代码吗?
Object.keys(byId) returns 与allIds完全相同的数组,但redux的目的是将所有数据存储尽可能扁平 一个对象,对吧?在对象外部调用 Object.keys() 方法而不保存它与此相反...
var array = {
posts: {
byId: {
"post1": {
id: "post1",
author: "user1",
body: "......",
comments: ["comment1", "comment2"]
},
"post2": {
id: "post2",
author: "user2",
body: "......",
comments: ["comment3", "comment4", "comment5"]
}
},
allIds: ["post1", "post2"]
},
comments: {
byId: {
"comment1": {
id: "comment1",
author: "user2",
comment: ".....",
},
"comment2": {
id: "comment2",
author: "user3",
comment: ".....",
},
"comment3": {
id: "comment3",
author: "user3",
comment: ".....",
},
"comment4": {
id: "comment4",
author: "user1",
comment: ".....",
},
"comment5": {
id: "comment5",
author: "user3",
comment: ".....",
},
},
allIds: ["comment1", "comment2", "comment3", "commment4", "comment5"]
},
users: {
byId: {
"user1": {
username: "user1",
name: "User 1",
},
"user2": {
username: "user2",
name: "User 2",
},
"user3": {
username: "user3",
name: "User 3",
}
},
allIds: ["user1", "user2", "user3"]
}
}
var keys = Object.keys(array.users.byId);
console.log(keys); // need to call a function where i should already know what i want
var simpleKeys = array.users.allIds
console.log(simpleKeys); // way faster too
Relevant links: