You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(BONUS) Linear Access - Object.keys.forEach vs Iterating Over Object.keys In a Regular For Loop
Random Access - Objects by ID vs Array search - n=100
Random Access - Objects vs Array search - n=1000
Random Access - Objects vs Array search - n=10000
Deleting - Objects (delete) vs Arrays (splice)
Conclusion
In the game, creating and deleting are relatively rare. Objects are only created once, but updated many, many times over the duration of their life. So the create/delete metrics are far less relevant. The vast majority of updates require linear access, for instance, updating objects once per frame.
The most surprising result here is that For n <= 100, arrays just as performant as objects for random access, even though the time complexity to find one is on the order of n.
Wherever linear updates are required (draw/fixed updates), use arrays. Iterate in reverse order, and if an item is dead, splice it from the array. To handle random updates (from the server, etc) have a repository of all game objects in an object. When the game object needs to be deleted, call it's delete function, which should delete it from the global container object, and set a flag indicating that it should be spliced from any arrays it's in.
The text was updated successfully, but these errors were encountered:
Code
Setup:
Creation:
Linear Access:
Random Access:
Deleting:
Results
Creating - Objects vs Arrays
For n=1000:
Linear Access - Objects vs Arrays
(BONUS) Linear Access - Object.keys.forEach vs Iterating Over Object.keys In a Regular For Loop
Random Access - Objects by ID vs Array search - n=100
Random Access - Objects vs Array search - n=1000
Random Access - Objects vs Array search - n=10000
Deleting - Objects (delete) vs Arrays (splice)
Conclusion
In the game, creating and deleting are relatively rare. Objects are only created once, but updated many, many times over the duration of their life. So the create/delete metrics are far less relevant. The vast majority of updates require linear access, for instance, updating objects once per frame.
The most surprising result here is that For n <= 100, arrays just as performant as objects for random access, even though the time complexity to find one is on the order of n.
Wherever linear updates are required (draw/fixed updates), use arrays. Iterate in reverse order, and if an item is dead, splice it from the array. To handle random updates (from the server, etc) have a repository of all game objects in an object. When the game object needs to be deleted, call it's delete function, which should delete it from the global container object, and set a flag indicating that it should be spliced from any arrays it's in.
The text was updated successfully, but these errors were encountered: