[1팀 강승훈] Chapter 4-2. 코드 관점의 성능 최적화 #35
Open
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.
과제 체크포인트
과제 요구사항
배포 주소
Promise.all이해)과제 셀프회고
1) API 호출 부분을 최적화주세요
2) 이미 호출한 API는 다시 호출하지 않도록 시도해보세요. (힌트: 클로저를 이용해서 캐시를 구성하면 됩니다.)
3) 불필요한 연산이 발생하지 않도록 해주세요
를 하기전에, 먼저 해당 SearchDialog가 무한스크롤로 동작해야 할 부분이 제대로 동작하지 않음을 발견했다.
console을 찍어보니 무한스크롤에 필요한
위 두개가 처음 모달이 열렸을 때 null값으로 잡히기 때문이었다.
이를 해결하기 위해, requestAnimationFrame을 사용하여 해결해주었다.
requestAnimationFrame은 브라우저의 다음 리페인트 전에 실행되므로 DOM 렌더링 후 실행을 보장하기에 setTimeout같은 처리보다 더 정확한 시점을 보장할 수 있다.
그럼 이제 다시
3) 불필요한 연산이 발생하지 않도록 해주세요
를 해보자.
불필요한 연산 차단: 인피니트 스크롤이 발생하여
page가 바뀔 때, 리액트는 의존성 배열을 확인한다.filteredLectures의 의존성인[lectures, searchOptions]는 변하지 않았으므로, 필터링 함수(filter)는 실행되지 않고 메모리에 저장된 값을 즉시 재사용.visibleLectures만 실행되어 이미 계산된 결과에서 100개, 200개씩 **잘라내기(slice)**만 수행4) 불필요한 렌더링 방지
5) “지연평가” 라는 키워드를 토대로 찾아보면 알 수 있답니다!
6) DnD 최적화
schedulesMap과tableId가 변경되지 않으면 이전 값 재사용한다.getColor함수 또한 useCallback으로 메모이제이션 해 주었다.변경 전
schedules를 props로 전달변경 후
useScheduleContext를 통해 전역 상태에서 직접 가져옴후기
다른 과제들보다 조금 더 명확한 과제라고 느껴졌습니다.
지연평가라는걸 새롭게 배웠습니다! 멋진 기술(?) 방법(?) 이라고 생각이 들었습니다. 정말 최적화라는 것에는 다양한 방법이 있구나~!
실제 프로젝트에서는 이만큼 최적화 할 내용이 없어서(?) 일수도 있지만, 이렇게 memo와 useMemo, useCallback을 많은 곳에 쓰는 게 맞나? 생각이 들었습니다.
리액트의 profiler가 여전히 낯설지만, 이를 잘 활용하여 실제 프로젝트에서도 더 나은 최적화를 가져가고 싶습니다.