zgrossbart / jdd

A semantic JSON compare tool
http://www.jsondiff.com
Apache License 2.0
1.03k stars 180 forks source link

Performance Enhancements #50

Closed Pluckerpluck closed 2 years ago

Pluckerpluck commented 2 years ago

So I accidentally removed some trailing whitespace in this. If I really have to I can go back and modify my commits to not include that, but right now I don't have time, so I'm just posting this for now as-is.


This pull request brings in a few changes that can greatly improve performance.

In order to benchmark the changes I used the following as input JSON: https://pokeapi.co/api/v2/pokemon/1/

I then created a second JSON file that added one extra pokemon at the start (and thus making every single element in the list incorrect)

On the current master branch the following times are recorded using chrome dev tools:

My changes bring these down to:

The first commit contains two changes:

  1. It moves the show call to after all the processing. This ensures that changes to the HTML do not cause reflows. (This change alone halves the time spend in processDiffs)
  2. It uses string manipulation rather than JQuery objects to construct the code blocks. (This change drops formatPRETags down by 70%)

The second second commit has the larger effect. The current system of processing diffs is slow because it performs a jquery lookup for every single line with a diff. If the number of lines in the file is long, this results in each lookup being slow, and with many diffs this can really add up.

My change caches this initial line lookup, and saves every single jquery object in a dictionary. This dictionary is then used to grab the lines of diffs, massively cutting down processing time.

zgrossbart commented 2 years ago

These updates are really smart and a big improvement. Thank you so much!!!