These are chat archives for ractivejs/ractive

3rd
Jul 2018
Curtis Taylor
@curtistee
Jul 03 2018 17:20
“Stupid performance trick” question for you Ractive mavens:
Is it “better" to change the attribute of an object inside a loop of n objects (object.property = newValue), then do Ractive.update(‘array’, array) or update each property using Ractive directly while in said loop, e.g. ractive.set(‘array.’ + index + ‘.property', newValue)?
Joseph
@fskreuz
Jul 03 2018 17:36
ractive.set() has a second argument that accepts a deep property. Doing this, you can construct an object with updated values and set it to the keypath in one go. Ractive will do a deep merge of the values.
This way, you don't have to do looping yourself.
Curtis Taylor
@curtistee
Jul 03 2018 17:40
Ah. I neglected to mention there are calculations involved inside the loop (which is actually map). So I’m updating each object’s property in the array. Thanks for pointing out the deep option though.
Chris Reeves
@evs-chris
Jul 03 2018 18:46
doing sets in a loop is definitely not as efficient as a deep set, but even more efficient would be to build a keypath map and set with it e.g. r.set({ 'array.0.key': v1, 'array.1.key': v2, ... })
that drops you to one runloop (and associated promise) and doesn't cause extra dirty checking
Curtis Taylor
@curtistee
Jul 03 2018 18:50
Hm. Given I need to loop in order to calculate each object’s property based on another value in that object, I don’t think I can avoid the loop. Plus, I have no idea of how many objects will be in the array without checking its size first. Are you suggesting I build the keypath map inside the loop dynamically, then set it once the loop’s executed?
I guess that’d be better than updating the entire array on the Ractive instance, but I haven’t dug deep into the source.
Chris Reeves
@evs-chris
Jul 03 2018 18:56
yep
Curtis Taylor
@curtistee
Jul 03 2018 18:56
Most excellent. Thanks for the tip, Chris!