I've seen multiple times people talking about O(1)
, O(n)
etc in questions but nobody tells what it means and how it works. I tried to search it online but I don't know what keywords to use..
What does these mean and how it applies to situations?
Please consider using these two simple examples and my questions in the spirit of this question.
Object:
- Time / performace difference for each level inward?
- Difference between
.object
vs['object']
in the concept of my question?
pets.dogs
or pets['dogs']
pets.dogs.collie
or pets['dogs']['collie']
pets.dogs.collie.size
or pets['dogs']['collie']['size']
- How does array affect this bunch (if I know the key)?
pets.dogs.collie.color[2]
and pets.dogs.collie.popularnames[56]
- How does array affect this bunch (if I don't know the key and this is in
for
loop)?
pets.dogs.collie.color[i]
and pets.dogs.collie.popularnames[i]
var pets = {
dogs: {
chihuahua: {
color: ["black", "white"],
size: "tiny",
hunting: false,
popularnames: [ /*100 names*/ ]
},
collie: {
color: ["brown", "white"],
size: "medium",
hunting: false,
popularnames: [ /*100 names*/ ]
},
// etc
},
cats: {
bengal: {
color: ["gray", "white"],
size: "medium",
fur: "short",
popularnames: [ /*100 names*/ ]
},
birman: {
color: ["black", "white"],
size: "medium",
fur: "long",
popularnames: [ /*100 names*/ ]
},
// etc
}
}
Bottom line question:
- Should I avoid complicated objects (e.g many, many selectors) and use multiple simpler ones?
- Is array iteration performance worse if I iterate array deep / 3-4 levels inside the object?
Array:
- Time / performace difference for each level inward (If I know the keys)?
pets[0]
pets[0][1]
pets[0][1][3]
- How does object affect this bunch (if I know the key)?
pets[0][1][1]['somekey']
var myArray = [
0: [
0: [
0: [ /* 100 objects */ ],
1: [ /* 100 objects */ ],
2: [ /* 100 ints */ ],
3: [ /* 100 ints */ ]
],
1: [
0: [ /* 100 objects */ ],
1: [ /* 100 objects */ ],
2: [ /* 100 ints */ ],
3: [ /* 100 ints */ ]
],
// etc
],
1: [
0: [
0: [ /* 100 objects */ ],
1: [ /* 100 objects */ ],
2: [ /* 100 ints */ ],
3: [ /* 100 ints */ ]
],
1: [
0: [ /* 100 objects */ ],
1: [ /* 100 objects */ ],
2: [ /* 100 ints */ ],
3: [ /* 100 ints */ ]
],
// etc
]
]
Bottom line question:
- Should I avoid complicated arrays (e.g many, many levels) and use multiple simpler ones?