Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.78k stars 215 forks source link

Improve array patches #276

Open orengriffin opened 3 years ago

orengriffin commented 3 years ago

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

Starcounter-Jack commented 3 years ago

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?

t-knapp commented 2 years ago

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

Starcounter-Jack commented 2 years ago

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.