fix: bug fix for highly correlated objects - #133
Open
jedluhmann wants to merge 19 commits into
Open
Conversation
Upgrades include bun, eslint, prettier, mocha, chai, and c8.
…tringify for array equality) Some comments are left in this commit to help clarify what is being optimized. These comments will be removed in the next commit. Changes: - Cache changes calculated in findMatchingObject() for later use in arrayDiff where op is 'equal' - Use JSON.stringify() to bypass diff logic when arrays are equal - findMatchingObject: Switched the order of arguments when calling this.diff() for consistency (candidate is the original value) and to allow caching (reuse change values calculated here in later fuzzy matching)
…condition immediately above for truthy values
…for failed tests Note: signature for deepEqual is assert.deepEqual(actual, expected, message)
Objective: Simplify the logic by giving each property a maximum possible score of 100 - Matching property: 50 points - Matching value: 50 points - Added/Deleted property: -50 points
Author
|
@andreyvit, please also note the new debug feature noted in #134. This is an incredibly useful addition. |
jedluhmann
force-pushed
the
jl-bug-fix-for-highly-correlated-objects
branch
2 times, most recently
from
September 3, 2026 22:34
ae0619a to
e0857c9
Compare
This updates the README with a simple diagram that provides a high level overview of how json-diff works.
jedluhmann
force-pushed
the
jl-bug-fix-for-highly-correlated-objects
branch
from
September 4, 2026 05:47
e0857c9 to
097dae0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug in the fuzzy logic that causes the array diffing logic to pick an incorrect winner for objects that have a high degree of similarity.
Test case that demonstrates this bug:
it 'should correctly pick best match based on similarity during scalarize, i.e. class obj2[2] should not be selected as best match for obj1[0], instead order should remain unchanged with added class property for obj1[0]'
See: https://github.com/jedluhmann/json-diff/blob/b6500e9d5669d9f31ebe51d26670dcbab2129bf3/test/json-diff.spec.js#L187
The objects in this test case consist of an array of 3 objects. The only difference between object A and object B is that the B0 contains a new property,
class: "prose". A human looking at the two objects can easily detect that a single property was added to the object at index 0. However, the algorithm was selecting A2 as the best match for B0, which resulted in completely replacing A0 with B0.Previously, this is how the score was determined:
The following table shows the fuzzy calculations:
Incorrect Output:
So why was this the case?
Comparing B0 to A0:
Comparing B0 to A3:
The results are now determined by the following:
The following table shows the new fuzzy calculations:
And the now Correct Output:
Also, the goal in making this change was to make the algorithm conceptually easier to reason about. Subtracting 30 here and adding 20 here and there and then assigning a value of 100 to a matching scalar value only to later run it through
score += Math.min(20, Math.max(-10, change.score / 5));is difficult to reason about from a conceptual standpoint.My approach was instead to think of each property of having a potential total of 100 points. This is conceptually much easier to grasp and also seems to work well. The only reduction in value that occurs due to recursion is
Math.max(0, 50 - change.score)and even this is much easier to reason about. This also allows the algorithm to be progressively enhanced, if needed, by considering how similar nested objects are to one another. However, the fuzzy matching that determines the object that is the best match seems to be a much more significant contribution than the recursive similarity of nested objects. And, if possible, leaving recursive similarity out of the equation makes the entire algorithm much, much easier to reason about.Let me know if you have any questions or need additional clarification.