-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(fe): optimize QuestionList, QuestionSection rendering perfor…
…mance (#42) * feat: add deep equality utility function * refactor: refactor QuestionList to use useEffect for question state management * refactor: optimize QuestionSection component with React.memo for performance * refactor: remove comment * refactor: remove console logs for question updates in QuestionList
- Loading branch information
Showing
4 changed files
with
90 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const isPrimitive = (value: unknown): boolean => { | ||
return value === null || (typeof value !== 'object' && typeof value !== 'function'); | ||
}; | ||
|
||
const areArraysEqual = (lhs: unknown[], rhs: unknown[]): boolean => { | ||
if (lhs.length !== rhs.length) return false; | ||
return lhs.every((item, index) => deepEqual(item, rhs[index])); | ||
}; | ||
|
||
const areObjectsEqual = (lhs: Record<string, unknown>, rhs: Record<string, unknown>): boolean => { | ||
const lhsKeys = Object.keys(lhs); | ||
const rhsKeys = Object.keys(rhs); | ||
|
||
if (lhsKeys.length !== rhsKeys.length) return false; | ||
|
||
return lhsKeys.every((key) => deepEqual(lhs[key], rhs[key])); | ||
}; | ||
|
||
export const deepEqual = (lhs: unknown, rhs: unknown): boolean => { | ||
if (lhs === rhs) return true; | ||
if (isPrimitive(lhs) || isPrimitive(rhs)) return false; | ||
|
||
if (Array.isArray(lhs) && Array.isArray(rhs)) { | ||
return areArraysEqual(lhs, rhs); | ||
} | ||
|
||
if (typeof lhs === 'object' && typeof rhs === 'object') { | ||
return areObjectsEqual(lhs as Record<string, unknown>, rhs as Record<string, unknown>); | ||
} | ||
|
||
return false; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './deep-equal'; |
This file contains 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
This file contains 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