Seeking ways to enhance the sorting of normalized objects based on a relationship. Imagine having an application that requires organizing data in a Vuex store containing multiple normalized objects, like this:
state: {
worms: {
3: { id: 3, name: 'Slurms McKenzie', measurements: [1, 6, 9] },
4: { id: 4, name: 'Memory worm', measurements: [3, 4, 12] },
6: { id: 6, name: 'Alaskan Bull Worm', measurements: [5, 7, 14]},
...
},
measurements: {
1: { id: 1, length: 5.2, timestamp: ...},
2: { id: 2, length: 3.4, timestamp: ...},
3: { id: 3, length: 5.4, timestamp: ...},
...
},
};
For instance, if there's a need to sort the worms
based on the timestamp
of their highest length achievement. To leverage Vue's reactivity, defining a getter for each worm would be ideal, like so:
const getters = {
longestLength: {
get() { return $store.getters
.measurements(this.measurements)
.sort(...)[0] },
},
timestampForLongest: {
get() { return this.longestLength.timestamp }
}
worm.extend(getters);
This setup allows for quick sorting based on timestampForLongest
, assuming the value is cached.
While there's a good starting point for calling this extend
method, several challenges exist:
- Current approach involves computing denormalized map and subsequent sorting, resulting in around 700ms latency on my Chrome browser with an 8th gen Intel processor; aiming to reduce this delay.
- Uncertainty surrounds manual invocation of Vue's reactivity system, potentially requiring definition of getters that trigger something like
measurement.__ob__.dep.depend()
. - Concern about Vue's capability to efficiently handle 800+ rows due to performance limitations or private API usage subject to changes.
- Finding a way to maintain the Vuex store (
$store
) within scope of getters, possibly resorting to arrow functions for clarity.
Is it feasible to compute and cache values as needed within plain JavaScript objects using Vue?