Skip to content
New issue

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

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

Already on GitHub? # to your account

Improve performances #39

Open
Toub opened this issue Aug 29, 2018 · 3 comments
Open

Improve performances #39

Toub opened this issue Aug 29, 2018 · 3 comments

Comments

@Toub
Copy link

Toub commented Aug 29, 2018

Is there a way to make rfc6902 faster?

See benchmark: https://github.com/Starcounter-Jack/JSON-Patch

@amiller-gh
Copy link

ref: #73

@williamstein
Copy link

(Caveat: I'm just a random user.)

I just took some real-world data from an application I'm working on (a nested JSON structure coming from a slatejs document tree, which JSON.stringify's to something with about 30K characters). I'm interested in computing diffs, so I took one edit operation (which slightly changes the tree), and compared performance of making a diff using rfc6902 versus https://github.com/Starcounter-Jack/JSON-Patch. The difference in performance is striking:

JSON-Patch was 30x-200x times faster than rfc6902 at this benchmark.

I hope this comment isn't too annoying since I'm not providing the exact JSON object. It's really just a random "in nature" JSON object that pops up when parsing some markdown. The benchmark code I ran is:

t = new Date(); for(i=0;i<100;i++){w=rfc.createPatch(cur, next);}; new Date() - t

where rfc = require("rfc6902") and cur and next are my two objects. With JSON-Patch, I ran the same loop:

t = new Date(); for(i=0;i<100;i++){w=jsonpatch.compare(cur, next);}; new Date() - t

Both libraries output equivalent rfc-6902 looking results in my tests, though of course things can be random in general. I tried other objects and got similar timings (where JSON-Patch is orders of magnitude faster).

I'm not asking for anything, and I realize that rfc6902 might be significantly better than JSON-Patch in terms of semantics, correctness, optimal patches (?), or some other metrics. I realize computing diffs is fraught with complexity. I just didn't expect such a huge difference in performance, and wanted to record that somewhere, in case it is helpful to somebody.

Looking more deeply: The source code of JSON-patch's function to compute a diffs looks like a straightforward recursive algorithm (it's short, but has basically no useful comments). The code in rfc6902 for computing diffs is here, and it's much prettier, vastly better commented, and appears to produces significantly more efficient patches in general. But these cost a lot more to produce in some cases.

@williamstein
Copy link

williamstein commented Jan 19, 2021

Just to add to the previous comment, I tried some more "real world examples", and indeed this rfc6902 produces dramatically better patches in practice on some examples. For one I just tried, rfc6902 produces a nice clean single add op, compared to JSON-patch producing a sequence of 37 operations to do the same thing. So indeed, you get what you pay for.

image

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

No branches or pull requests

3 participants