JSON-Patch icon indicating copy to clipboard operation
JSON-Patch copied to clipboard

Improve array patches

Open orengriffin opened this issue 3 years ago • 3 comments

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

orengriffin avatar Jun 20 '21 19:06 orengriffin

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?

Starcounter-Jack avatar Aug 13 '21 10:08 Starcounter-Jack

I face exactly the same problem when I compare two arrays.

t-knapp avatar Feb 21 '22 08:02 t-knapp

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.

Starcounter-Jack avatar Mar 24 '22 17:03 Starcounter-Jack