Two.Group.getById() optimization #770
Merged
+3
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The name of Two.Group.getById() somewhat implies it would use the Two.Group.children.ids map but actually just loops through every child until found, which can decrease performance if you need to grab many objects from a large group by id and are unaware.
The optimization checks if the object is present in the ids map before looping through all children, and returns the item if found. It still reverts to the old way of checking if the object isn't found in the ids map, so nested objects and objects not in the id map will still be found.
Validated the speed increase with the test cases here: https://github.com/Aries1542/two.js-speed-test.
For those cases, Two.Group.getById() went from around 30x as slow to about equal to Two.Group.children.ids[] for random cases and 60x as slow to about equal in the worst case.
I wasn't able to get your tests running on my machine however so that should probably still be checked.