mutative
mutative copied to clipboard
Performance of Reads on Draft
As part of an SDK I'm working on I provide a draft version of user provided data structure back to them to be updated. I want to maintain the immutable status of the original data so that I can compare it safely later on.
However, in this case the user has provided a reasonably large data structure that represents a physics engine. The performance of making changes to the mutative draft is understandably not as fast as a raw object - however, the performance of reads of properties of the draft seem to be impacted too.
To test this out I've put together a simple standalone test case over here: https://github.com/kevglass/mutative-performance-sample/ - it's important to note that the create() is intentionally inside the loop since in the real system the draft is created every frame.
It simulates a collection of balls (30) on a table moving in random directions and colliding. The performance test can be run in two ways - either with writing to the draft object (the same as it would be in a real physics engine) or in read only mode where the simulation is just calculating some values based on the contents of the draft objects.
I feel like I must be doing something wrong but I can't quite understand what it is. The results on my M1 for read only access to the draft object looks like this:
2024-03-11T21:23:43.254Z
Iterations=5000 Balls=30 ReadOnly=true
RAW : 5000 iterations @12ms (0.0024 per loop)
RAW+COPY: 5000 iterations @254ms (0.0508 per loop)
MUTATIVE: 5000 iterations @3709ms (0.7418 per loop)
IMMER : 5000 iterations @4309ms (0.8618 per loop)
Where RAW is a simple JS object, RAW+COPY is taking a copy of the object at each stage (parse/stringify). Mutative is the lovely library here and Immer for comparison.
I hadn't expected the impact of reading from the draft to be so high, so i'm guessing I've done something very wrong.
Any thoughts or directions appreciated.
For completeness heres my read/write results from my M1:
RAW : 5000 iterations @14ms (0.0028 per loop)
RAW+COPY: 5000 iterations @270ms (0.054 per loop)
MUTATIVE: 5000 iterations @4813ms (0.9626 per loop)
IMMER : 5000 iterations @5430ms (1.086 per loop)