I have a large JavaScript object that contains properties that are added and deleted as and when required. Something similar to this:
var data = {
data1: {
subscribers : ["sub1", "sub2"]
},
data2: {
subscribers : ["sub1", "sub2"]
}
//[0...*]
};
I want the subscribers from “data2” so I use data["data2"].subscribers to access this information.
But my questions are:
- What are JavaScript internal workings of accessing properties like this (dot notation, using the property name)?
- Does the size of the object have an effect on how fast the subscribers array is returned?
My guess would be that the size would effect the speed of receiving the returned data because I think the way that JavaScript accesses the properties is using a for loop over the objects:
function getSubs(name) {
for(var prop in data) {
if(prop === name) {
return data[prop].subscribers;
}
}
}
var subs = getSubs("data2");
No, it doesn’t work by iterating over all the properties but basically by using the frequently used hash table pattern before compilation and classes after compilation (read more here regarding V8).
In fact, that’s part of the internal working of javascript engines, not the specification. What you need to know is :
Yes, having many properties may have an impact but much lighter than what you seem to think. Don’t worry about it.