Skip to content

⚡ [performance] Replace slow JSON.stringify with deepEqual in setConfig#2

Open
Elathi wants to merge 1 commit into
mainfrom
perf-optimize-string-serialization-17939495821773399236
Open

⚡ [performance] Replace slow JSON.stringify with deepEqual in setConfig#2
Elathi wants to merge 1 commit into
mainfrom
perf-optimize-string-serialization-17939495821773399236

Conversation

@Elathi

@Elathi Elathi commented Apr 9, 2026

Copy link
Copy Markdown
Owner

Replaced slow JSON.stringify deep object equality check in the setConfig function with a more efficient deepEqual utility.

💡 What:

  • Implemented a performant deepEqual function in utils.ts that handles objects, arrays, and primitives.
  • Updated App.tsx to use deepEqual instead of JSON.stringify in the setConfig callback.

🎯 Why:
Using JSON.stringify for deep equality is an expensive operation that requires serializing the entire object graph. In the hot path of setConfig (which can be called during drag operations), this serialization was a major bottleneck, especially as the number of elements in PaperConfig grew.

📊 Measured Improvement:
Using a benchmark script with a configuration containing 1000 elements:

  • Same objects: deepEqual is ~2x faster than JSON.stringify.
  • Early difference: deepEqual is ~490x faster because it short-circuits immediately when a difference is found, whereas JSON.stringify must always serialize the entire object.

This change ensures that state updates remain smooth and efficient, particularly during user interactions.


PR created automatically by Jules for task 17939495821773399236 started by @Elathi

Replaced JSON.stringify-based deep comparison with a dedicated deepEqual
utility in the setConfig hot path. This avoids unnecessary string
serialization of the potentially large PaperConfig object, especially
the elements array, during frequent updates like dragging.

Benchmark results for 1000 iterations with 1000 elements:
- Identical objects: ~2x faster
- Early difference: ~490x faster

Functional correctness verified with test suite.

Co-authored-by: Elathi <70385492+Elathi@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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

Successfully merging this pull request may close these issues.

1 participant