프론트엔드 개발자를 위한, 실전 웹 성능 최적화 Part.2 (인프런)
인프런 동영상 강의 내용을 요약 정리하면서 추가적으로 참고한 내용을 덧붙였습니다.
<img src="puppy.jpg" width="640" height="360" alt="강아지와 풍선" />
width
, height
속성을 기반으로 종횡비를 계산하여 레이아웃 계산시에 이 정보를 제공한다.img { aspect-ratio: attr(width) / attr(height); }
<link rel=preload>
사용하고 font-dispaly: optional
과 조합하여 사용import React from 'react';
import ReactDOM from 'react-dom';
import LazyLoad from 'react-lazyload';
import MyComponent from './MyComponent';
const App = () => {
return (
<div className="list">
<LazyLoad height={200}>
<img src="tiger.jpg" />
</LazyLoad>
<LazyLoad height={200} once>
<MyComponent />
</LazyLoad>
<LazyLoad height={200} offset={100}>
<MyComponent />
</LazyLoad>
<LazyLoad>
<MyComponent />
</LazyLoad>
</div>
);
};
ReactDOM.render(<App />, document.body);
const { number, diff } = useSelector(state => ({
number: state.counter.number,
diff: state.counter.diff
}));
const number = useSelector(state => state.counter.number);
const diff = useSelector(state => state.counter.diff);
const { number, diff } = useSelector(
state => ({
number: state.counter.number,
diff: state.counter.diff
}),
shallowEqual
);
// 문제가 발생하는 코드, filter에 의해 매번 새로운 배열이 할당되므로 리렌더링이 발생
const users = useSelector(
(state) => state.users.filter(user => user.subscribed)
);
reselect
또는 redux-toolkit
의 createSelector
를 사용하여 해결할 수 있다.// userSelector.jsx
import { createSelector } from 'reselect';
const userSelector = createSelector(
state => state.users,
users => users.filter(user => user.subscribed)
)
export default userSelector;
// App.jsx
import { useSelector } from 'react-redux';
import userSelector from '../selector/userSelector'
function App = () => {
const user = useSelector(selectFilteredPhotos);
// ...
}
const width = imgElement.naturalWidth || imgElement.offsetWidth || imgElement.width;
const height =imgElement.naturalHeight || imgElement.offsetHeight || imgElement.height;
canvas.width = width / 3;
canvas.height = height / 3;
context.drawImage(imgElement, 0, 0, canvas.width, canvas.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
const length = imageData.length;