问题
In some of the projects I'm working on as part of my day job, I need to access data in very large JS objects (on the order of thousands of key-value pairs). I'm trying to improve the efficiency of my code, so I came up with a few questions:
- What is the runtime complexity of JS when accessing a field in such an object? My initial hunch is that it's O(n)
- Is there a difference when accessing through dot notation or bracket notation? (e.g.
obj.fieldvsobj[field]) - I'm guessing there is a different answer for different runtime engines - is there a place where I can see the difference between them?
回答1:
Javascript objects are actually Hashes, so the complexity is O(1) for all engines.
obj.field is an alias for obj['field'], so they have the same performances.
You can find some JS hashes performance tests here, unfortunately only for your browser engine.
来源:https://stackoverflow.com/questions/45266944/complexity-of-accessing-data-in-an-object