Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inefficient patch encoding when prepending to arrays #282

Open
stbrody opened this issue Sep 13, 2021 · 2 comments
Open

Inefficient patch encoding when prepending to arrays #282

stbrody opened this issue Sep 13, 2021 · 2 comments

Comments

@stbrody
Copy link

stbrody commented Sep 13, 2021

When adding an element to the beginning of an array, the resulting patch contains the full contents of the array, thus the storage requirements of the resulting patch scales with the size of the original array, not with the size of the diff between the arrays. Compare this with adding an element to the end of an array, where the resulting patch only contains data that scales with the size of the new element being added.

> fjp = require('fast-json-patch')
> fjp.compare(['foo', 'bar'], ['foo', 'bar', 'baz'])
[ { op: 'add', path: '/2', value: 'baz' } ]
> fjp.compare(['foo', 'bar'], ['baz', 'foo', 'bar'])
[
  { op: 'replace', path: '/1', value: 'foo' },
  { op: 'replace', path: '/0', value: 'baz' },
  { op: 'add', path: '/2', value: 'bar' }
]
@holynewbie
Copy link

not only at the beginning, but at any position.

const fjp = require('fast-json-patch');
fjp.compare([1, 3], [1, 2, 3]);

/**
this will result 

[
  {
    "op": "replace",
    "path": "/1",
    "value": 2
  },
  {
    "op": "add",
    "path": "/2",
    "value": 3
  }
]
*/

@stbrody
Copy link
Author

stbrody commented Oct 2, 2022

Hi, just wondering if there's any update on this? It seems like it should be possible to encode these types of updates more efficiently, for example with a new "insert" operator that just takes the value and array index to insert the value at, and implicitly pushes back the index of all later elements in the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants