Skip to content

perf: replace JSON stringify/parse deep equality with recursive comparison#1927

Open
natsukingly wants to merge 1 commit intox402-foundation:mainfrom
natsukingly:perf/deep-equal-optimization
Open

perf: replace JSON stringify/parse deep equality with recursive comparison#1927
natsukingly wants to merge 1 commit intox402-foundation:mainfrom
natsukingly:perf/deep-equal-optimization

Conversation

@natsukingly
Copy link
Copy Markdown

Summary

deepEqual in @x402/core used JSON.stringify → JSON.parse → JSON.stringify for comparison, creating unnecessary intermediate objects. This PR replaces it with a direct recursive comparison.

Changes

  • Rewrote deepEqual to use recursive structural comparison
  • Eliminates double JSON serialization/deserialization overhead
  • Handles all existing edge cases: primitives, null, undefined, arrays, nested objects, key-order independence

Before

const normalize = (obj) => {
  // JSON.stringify → JSON.parse → JSON.stringify for each nested value
};
return normalize(obj1) === normalize(obj2);

After

// Direct recursive comparison — no serialization
if (Array.isArray(obj1)) return obj1.every((item, i) => deepEqual(item, obj2[i]));
return keys1.every(key => deepEqual(obj1[key], obj2[key]));

Test plan

  • Existing utils.test.ts tests pass
  • Full monorepo build passes

@github-actions github-actions bot added typescript sdk Changes to core v2 packages labels Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sdk Changes to core v2 packages typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant