-
Notifications
You must be signed in to change notification settings - Fork 641
Description
Bug report
I have a simple model containing an array of ~10K randomly generated points (points are a very simple model with an identifier
and a string field).
I'm replacing the array with another similar-sized array of randomly-generated points. (In the real app this would be data coming from the server)
The replace itself takes a very, very long time - about 16 seconds on my machine.
I tried to understand why, I think this is due to the attempts of entity reconciliation in the array. It has 2 nested for loops, resulting in O(n^2) iterations.
I'm not sure why this is needed though, as the entities contain IDs and can be looked up quickly - Is there a reason for this? Am I doing something wrong?
const Point = types.model("Point").props({
uuid: types.identifier,
name: types.string,
});
const PointsStore = types
.model({
points: types.array(Point),
})
.actions((self) => ({
setPoints(points: Instance<typeof Point>[]): void {
self.points.replace(points);
},
}));
const store = PointsStore.create({
points: generateRandomPoints(10000),
});
const newPoints = generateRandomPoints(10000).map((e) => Point.create(e));
const before = new Date();
// store.setPoints([]); // If I include this, replacing is fast
store.setPoints(newPoints); // <-- This is very slow!
if I insert the line
store.setPoints([]);
before assigning the new array, there's no performance issue. This is a workaround though, I don't want to always be conscious of this issue when replacing arrays.
- I've checked documentation and searched for existing issues and discussions
- I've made sure my project is based on the latest MST version
- Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code
https://codesandbox.io/p/sandbox/check-mst-repalce-performance-forked-2jvqwj?workspaceId=ws_UCwiGtoQYRJP5RUk9PpyQL
** What should happen? **
setPoints
should take a reasonable amount of time
** What happens instead? **
setPoints
is agonizingly slow, takes 16sec on my machine