Reactのパフォーマンス
#WIP
https://vercel.com/blog/how-react-18-improves-application-performance
react v18
vercelのきじ
Reactのパフォーマンスについて
https://kentcdodds.com/blog/usememo-and-usecallback
MOST OF THE TIME YOU SHOULD NOT BOTHER OPTIMIZING UNNECESSARY RERENDERS. React is VERY fast and there are so many things I can think of for you to do with your time that would be better than optimizing things like this.
ほとんどの場合、不要なレンダリングの最適化は気にしなくていい
パフォーマンスに役立つReact Hooks
Why Did You Render
パフォーマンス
開発モードでの計測
気をつけること
プロファイルの結果は相対的
本番環境のほうが早いが、相対的に遅い部分を見つけることができる
手順
1. React DevToolsを含む全てのChrome拡張を無効にする
2. dev toolの「パフォーマンス」タブでRecordをする
3. プロファイル対象のアクションを実行する
20秒以内に収めること。
4. Recordを停止する
5. ReactイベントがUser Timingラベルの下にグループ化される
本番モードでの計測
気をつけること
ベンチマークテストを行う場合は本番用ビルドで行う
reactのchrome拡張の色が赤だったら開発用、黒だったら本番用
https://gyazo.com/95deeb80981e62219b806fa2ca3bcf62 https://gyazo.com/89ef2aaf0a5a3eb6d3e2630cd9b29860
パフォーマンス計測のための手順
ビルド: $ npm run build
その後表示されるコマンドを実行する
yarn global add serve
serve -s build
計測の手法
dev toolsのperformance > Recordを使う
dev toolsのHilight Updatesを使って再描画されてる具合を調べる
ベンチマークテスト
react-addons-perfを使う
$ npm i -D react-addons-perf @types/react-addons-perf
準備
プロジェクトのhogehogeに以下をセット
code:ts
import * as Perf from 'react-addons-perf';
window.Perf = Perf;
計測 ref
1. コンソールでPerf.start()を実行(計測開始)
2. 計測したい操作を行う
3. コンソールでPerf.stop()を実行(計測終了)
4. コンソールでPerf.printWasted()を実行(結果出力)
参考
https://reactjs.org/docs/perf.html
http://js.studio-kingdom.com/react/guides/perf
https://hfuji.hatenablog.jp/entry/2017/06/23/220208
Reactを使ったプロダクトのパフォーマンスを改善した話 | GMOメディア エンジニアブログ
lighthougeを使う
chrome dev toolsのAuditタブから「Run audits」で計測
参考
Webpagetestでサイトのパフォーマンスを定期計測する - アクトインディ開発者ブログ
未読 WebPagetestをご存知ですか? - Akamai Japan Blog
Chrome Developer Toolsを使う
Chrome Developer Toolsでパフォーマンス計測・改善 - Qiita
Chrome DevToolsを用いたメルカリWebのパフォーマンス計測 - Mercari Engineering Blog
React特有のテクニック
長いリストの仮想化 ref
react-virtualized, react-windowなどを用いる
リコンシリエーションを避ける
無駄な再描画を避ける
props,stateインスタンスが変化したとき、その中身の値が全く同じでも再レンダリングされる
選択肢
shouldComponentUpdate
PureComponent
useMemo
React.memo
以下のような場合分けで考える ref
1. propsがないとき
class
FC
React.memoか普通のFC
ここによると大差ないらしい
2. propsがshallow equalで対応しきれるとき
class
FC
3. propsがdeep equalで対応する必要があるとき
class
FC
4. 変化しうるプロパティが特定できているとき
class
FC
Listのkeyを適切に付与する
https://reactjs.org/docs/profiler.html
公式のプロファイラ
レンダリング時間を細かく出力してkルエル
https://mrtry.hatenablog.jp/entry/2019/12/12/231454
rendering観測
https://qiita.com/nabeliwo/items/de0bc076ed7105dda2ca
参考
パフォーマンス最適化 – React
未読 https://yosuke-furukawa.hatenablog.com/entry/2017/09/28/094430
React製のSPAのパフォーマンスチューニング実例 | リクルートテクノロジーズ メンバーズブログ
books 未読
Webフロントエンド パフォーマンス改善ハンドブックを公開しました - dwango on GitHub
Introduction · Webフロントエンド パフォーマンス改善ハンドブック
https://medium.com/@gauravsapiens/react-native-making-your-app-fast-again-d00d02ee2b2a
これの2番目のpropsに直接関数渡すなって話、FCでも言えるのかな?
言える。
useWhyDidYouUpdateなどを使えばわかる
対策するためにはuseCallbackを使えばいい
ちょうどそういう話がここに書いている ref
classではそういうものだということはとりあえずわかった
仕組みをちゃんと理解していないのでわからない
https://ja.reactjs.org/docs/faq-functions.html#is-it-ok-to-use-arrow-functions-in-render-methods
公式
https://houssein.me/progressive-react
https://qiita.com/teradonburi/items/5b8f79d26e1b319ac44f
https://speakerdeck.com/maxmellon/reactzhi-spa-niokeru-pahuomansutiyuningu?slide=20
https://speakerdeck.com/kkoudev/frontend-pagespeed-insights?slide=5
https://speakerdeck.com/fumitoito/fei-tong-qi-uimiao-hua-niyorugao-su-naapurikesiyonfalseshi-zhuang?slide=2
https://speakerdeck.com/maxmellon/react-performance-tuning?slide=26
https://itnext.io/react-optimization-tips-224c66b4b30d
https://times.hrbrain.co.jp/entry/react-hooks-performance#これまでのチューニング方法
https://kentcdodds.com/blog/profile-a-react-app-for-performance
本番環境で計測しよう、という話
https://recruit-tech.co.jp/blog/2018/09/19/react_spa_performance_tuning/
https://tech.jxpress.net/entry/2021/08/04/140249
新し目の記事