Skip to content

FlatList renders items way too often, resulting in poor performance #29946

@dirkpostma

Description

@dirkpostma

Description

<FlatList> should enhance the performance of rendering lists with lots of items. Unfortunately, there (still) exists a bug that leads to way too many re-renders of list items, resulting in poor performance.

Code snippet:

const App = () => {
  const data = generateData(20);

  return (
    <FlatList
      data={data}
      renderItem={({index}) => {
        console.log('renderItem', index);
        return <View style={styles.listItem} />;
      }}
    />
  );
};
[Mon Sep 14 2020 23:10:28.303]  LOG      Running "TryFlatList" with {"rootTag":801,"initialProps":{}}
[Mon Sep 14 2020 23:10:28.305]  LOG      renderItem 0
[Mon Sep 14 2020 23:10:28.305]  LOG      renderItem 1
[Mon Sep 14 2020 23:10:28.306]  LOG      renderItem 2
[Mon Sep 14 2020 23:10:28.306]  LOG      renderItem 3
[Mon Sep 14 2020 23:10:28.307]  LOG      renderItem 4
[Mon Sep 14 2020 23:10:28.308]  LOG      renderItem 5
[Mon Sep 14 2020 23:10:28.309]  LOG      renderItem 6
[Mon Sep 14 2020 23:10:28.309]  LOG      renderItem 7
[Mon Sep 14 2020 23:10:28.310]  LOG      renderItem 8
[Mon Sep 14 2020 23:10:28.311]  LOG      renderItem 9
[Mon Sep 14 2020 23:10:28.312]  LOG      renderItem 0.   <=  unnecessary render of item #0
[Mon Sep 14 2020 23:10:28.313]  LOG      renderItem 1.        following 9 also
[Mon Sep 14 2020 23:10:28.313]  LOG      renderItem 2
[Mon Sep 14 2020 23:10:28.314]  LOG      renderItem 3
[Mon Sep 14 2020 23:10:28.315]  LOG      renderItem 4
[Mon Sep 14 2020 23:10:28.316]  LOG      renderItem 5
[Mon Sep 14 2020 23:10:28.317]  LOG      renderItem 6
[Mon Sep 14 2020 23:10:28.317]  LOG      renderItem 7
[Mon Sep 14 2020 23:10:28.318]  LOG      renderItem 8
[Mon Sep 14 2020 23:10:28.319]  LOG      renderItem 9
[Mon Sep 14 2020 23:10:28.319]  LOG      renderItem 10
[Mon Sep 14 2020 23:10:28.320]  LOG      renderItem 11
[Mon Sep 14 2020 23:10:28.320]  LOG      renderItem 12
[Mon Sep 14 2020 23:10:28.321]  LOG      renderItem 13
[Mon Sep 14 2020 23:10:28.322]  LOG      renderItem 14
[Mon Sep 14 2020 23:10:28.323]  LOG      renderItem 15
[Mon Sep 14 2020 23:10:28.324]  LOG      renderItem 16
[Mon Sep 14 2020 23:10:28.324]  LOG      renderItem 17
[Mon Sep 14 2020 23:10:28.325]  LOG      renderItem 18
[Mon Sep 14 2020 23:10:28.326]  LOG      renderItem 19
[Mon Sep 14 2020 23:10:40.329]  LOG      renderItem 0  <=  again render of item #0
[Mon Sep 14 2020 23:10:40.331]  LOG      renderItem 1
[Mon Sep 14 2020 23:10:40.333]  LOG      renderItem 2
[Mon Sep 14 2020 23:10:40.334]  LOG      renderItem 3
[Mon Sep 14 2020 23:10:40.335]  LOG      renderItem 4
[Mon Sep 14 2020 23:10:40.336]  LOG      renderItem 5
[Mon Sep 14 2020 23:10:40.337]  LOG      renderItem 6
[Mon Sep 14 2020 23:10:40.338]  LOG      renderItem 7
[Mon Sep 14 2020 23:10:40.338]  LOG      renderItem 8
[Mon Sep 14 2020 23:10:40.339]  LOG      renderItem 9
[Mon Sep 14 2020 23:10:40.340]  LOG      renderItem 10
[Mon Sep 14 2020 23:10:40.341]  LOG      renderItem 11
[Mon Sep 14 2020 23:10:40.341]  LOG      renderItem 12
[Mon Sep 14 2020 23:10:40.342]  LOG      renderItem 13
[Mon Sep 14 2020 23:10:40.343]  LOG      renderItem 14
[Mon Sep 14 2020 23:10:40.343]  LOG      renderItem 15
[Mon Sep 14 2020 23:10:40.344]  LOG      renderItem 16
[Mon Sep 14 2020 23:10:40.345]  LOG      renderItem 17
[Mon Sep 14 2020 23:10:40.346]  LOG      renderItem 18
[Mon Sep 14 2020 23:10:40.346]  LOG      renderItem 19

This is on first mount, without having done any scrolling.

Related to:

Workaround

There is a workaround to solve performance issue: wrap the ListItems in React.memo (or use PureComponent or implement componentShouldUpdate). However, this is just a workaround and not the way it should work.

React Native version:

yarn react-native info
yarn run v1.22.4
$ /Users/dirkpostma/development/playground/TryFlatList/node_modules/.bin/react-native info
info Fetching system and libraries information...
System:
    OS: macOS 10.15.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 1.24 GB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 10.14.2 - /var/folders/p8/lc8f03hj4glfwjx1c0_39d8w0000gn/T/yarn--1600099516621-0.65803085701635/node
    Yarn: 1.22.4 - /var/folders/p8/lc8f03hj4glfwjx1c0_39d8w0000gn/T/yarn--1600099516621-0.65803085701635/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.14.2/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /Users/dirkpostma/.gem/ruby/2.4.9/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.7, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 19, 23, 27, 28, 29
      Build Tools: 27.0.3, 28.0.3, 29.0.2, 29.0.3, 30.0.0
      System Images: android-19 | Google APIs Intel x86 Atom, android-21 | Intel x86 Atom, android-21 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6308749
    Xcode: 11.7/11E801a - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_242 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.63.2 => 0.63.2
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found
✨  Done in 3.53s.

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. git clone https://github.com/dirkpostma/react-native-flatlist-demo
  2. yarn
  3. cd ios; pod install; cd ..
  4. yarn ios
  5. inspect logs

Expected Results

On first mount, I would expect that all items would only be rendered at most once.

Snack, code example, screenshot, or link to a repository:

https://github.com/dirkpostma/react-native-flatlist-demo

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions