Fix JSON diff viewer performance issue causing UI freeze with large p… - #19
Conversation
…ayloads Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the JSON diff viewer to handle large payloads by implementing virtual rendering with chunked loading to prevent UI freezes. The key improvements focus on performance enhancements that allow the component to display 15k+ lines of data smoothly.
- Implements chunked rendering with progressive loading using
requestIdleCallback - Optimizes core diff computation functions by replacing array methods with for-loops and improving data structures
- Refactors render logic to use iterative approaches instead of functional array methods
Reviewed Changes
Copilot reviewed 4 out of 8 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/index.tsx | Adds chunked rendering state management and progressive loading logic to prevent UI blocking |
| src/compute-lines.ts | Optimizes diff computation performance by replacing forEach/map with for-loops and using Set for lookups |
| lib/index.js | Compiled JavaScript output reflecting the TypeScript changes in index.tsx |
| lib/compute-lines.js | Compiled JavaScript output reflecting the TypeScript optimizations in compute-lines.ts |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if ( | ||
| visibleNodes.length < computeLineInformation( | ||
| oldValue, | ||
| newValue, | ||
| this.props.noise, | ||
| this.props.disableWordDiff, | ||
| this.props.compareMethod, | ||
| this.props.linesOffset, | ||
| ).lineInformation.length | ||
| ) { |
There was a problem hiding this comment.
The computeLineInformation function is called on every render to check if more chunks need loading. This expensive computation should be cached or moved to avoid repeated calculations during progressive rendering.
| requestIdleCallback?.(() => { | ||
| this.setState((prev) => ({ | ||
| renderedChunks: prev.renderedChunks + 1, | ||
| })); | ||
| }); |
There was a problem hiding this comment.
Multiple requestIdleCallback calls could be queued simultaneously during fast renders, leading to unnecessary state updates. Consider debouncing or checking if a callback is already pending before scheduling another one.
| if ((oldString.startsWith('{') || oldString.startsWith('[')) && | ||
| (newString.startsWith('{') || newString.startsWith('['))) { | ||
| try { | ||
| JSON.parse(oldString); | ||
| JSON.parse(newString); | ||
| isJSON = true; | ||
| } catch { | ||
| isJSON = false; |
There was a problem hiding this comment.
JSON detection logic is too simplistic. Strings that start with '{' or '[' but aren't valid JSON will still attempt JSON parsing, potentially causing performance issues with large invalid JSON-like strings. Consider adding a more robust JSON detection or limiting the size of strings that undergo JSON parsing attempts.
|
@sneha0099 Please check the failing tests and look into the copilot comments |
Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
|
Key Changes:
Optimized Diff Computation
|
|
Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
Screen.Recording.2025-09-08.172756.mp4
Screen.Recording.2025-09-09.231846.mp4 |
Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
|
@sneha0099 please look into the failing pipeline |
Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
Signed-off-by: Sneha Prajapati <snehaprajapati678@gmail.com>
manasmanohar
left a comment
There was a problem hiding this comment.
@sneha0099 could you please fix the failing pipeline as well with this pr. Having the test pipeline will help catch bugs earlier going ahead.
|
it is giving this what can be the issue? Have you any idea because it's only giving something went wrong. |
Fixes #18
Screen.Recording.2025-09-03.011242.online-video-cutter.com.mp4