JSON-Patch
JSON-Patch copied to clipboard
Improve array patches
we at Optibus have an issue with patches that are created when two arrays are compared - the second is a filter result of the first. instead of having a simple remove patch, we can get a few hundred (or even thousands) of replace patches.
the suggested code, using lodash's 'difference', is trying to detect those cases and use a simple 'remove' or 'add' patch. it also reverts to old code in cases where: a. the result is simply wrong - mostly like the difference between the arrays is a big one. b. the differences are so great there is probably no added value.
I added 1000 tests with random arrays. so i believe the solution is solid. regarding the tests that fail - i would consider removing them
a. the result is simply wrong - mostly like the difference between the arrays is a big one. This sounds alarming. Wrong as in producing a big set of operations (not capturing the intention of the patch or not optimizing for operation set size), or wrong as in producing a patch that does not render the correct array? Any algorithm must be sound and correct.
b. the differences are so great there is probably no added value.
What is the heuristics of evaluating when the lodash algorithm is appropriate?
I face exactly the same problem when I compare
two arrays.
This would be a major change. If you can provide a simple plugin system such that existing users are not impacted for size and performance we could accept this as a standard feature.