{"id":66743,"date":"2024-10-18T13:37:42","date_gmt":"2024-10-18T08:07:42","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=66743"},"modified":"2024-10-23T22:34:36","modified_gmt":"2024-10-23T17:04:36","slug":"reduce-rendering-time-with-large-data-in-react","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/reduce-rendering-time-with-large-data-in-react\/","title":{"rendered":"Reduce rendering time with large data in\u00a0react"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>Performance is key in modern <a href=\"https:\/\/www.tothenew.com\/digital-engineering\/web-development\">web development<\/a>. Managing efficient rendering becomes crucial as web applications grow in complexity and data volume. One effective technique for improving performance in <a href=\"https:\/\/www.tothenew.com\/react-js-development-consulting-company\">React applications<\/a> is virtualization. This article will explore virtualization, why it matters, and how to implement it in a React application.<\/p>\n<h3>What is Virtualization?<\/h3>\n<p>In web development, virtualization refers to rendering only the visible portion of a large data set to the DOM while keeping the rest in memory. This approach significantly reduces the number of DOM nodes, enhancing rendering performance and improving the overall user experience.<\/p>\n<h3>Problem Overview<\/h3>\n<p>When dealing with large lists or grids in React, rendering all components at once can lead to several performance issues. Here\u2019s a breakdown of the common problems developers face:<\/p>\n<ul>\n<li><strong>Excessive DOM Updates<\/strong><br \/>\nReact renders all components in a list by default, and if that list is large (for example, thousands of items), this results in a large number of DOM nodes being created. Such a large volume of DOM updates can cause performance bottlenecks, making the application sluggish.<\/li>\n<li><strong>Increased Memory Usage<\/strong><br \/>\nLoading all the components in a large list at once can consume a significant amount of memory. Browsers may struggle to manage such a large number of DOM elements, resulting in crashes or unresponsive pages.<\/li>\n<li><strong>Long Initial Load Times<\/strong><br \/>\nWithout using virtualization, React renders the entire list or grid as soon as the component mounts, leading to longer load times. This can create a poor user experience, as users might see blank screens or experience delays in loading when they expect immediate interaction.<\/li>\n<li><strong>Unresponsive Scrolling<\/strong><br \/>\nRendering large lists all at once can cause the UI to become unresponsive during scrolling. Since React has to update the DOM for every scroll event, users may experience laggy or delayed scrolling behaviour, often referred to as \u201cjanky scrolling.\u201d<\/li>\n<\/ul>\n<h3>How to Tackle Rendering Large Lists in React?<\/h3>\n<p>If you end up in a place where you have to render tens of thousands of complex list items, you would inevitably have to use one of the following solutions, no matter how capable your end user\u2019s hardware is. Firstly we\u2019ll start with a general solution that everyone can incorporate into their code:<\/p>\n<h3>Why Virtualization Matters: Trade-offs<\/h3>\n<p>While virtualization drastically improves performance, it\u2019s important to understand the trade-offs:<\/p>\n<ul>\n<li><strong>Initial Complexity<\/strong>: Implementing virtualization adds complexity to your codebase compared to a simple list rendering. However, tools like react-virtualized mitigate this with pre-built components.<\/li>\n<li><strong>Scrolling Behavior<\/strong>: Virtualization renders only a subset of list items at a time, which can result in some items briefly disappearing during fast scrolling, depending on your settings.<\/li>\n<li><strong>Overhead<\/strong>: Virtualization introduces some overhead by calculating which items are visible and managing off-screen data. Although this is minimal compared to rendering all elements, it\u2019s worth considering.<br \/>\nUltimately, the performance gains far outweigh these trade-offs for lists with over a few hundred items.<\/li>\n<\/ul>\n<h3>Performance Benchmark: Before and After Virtualization<\/h3>\n<p>Before &#8211;<\/p>\n<div id=\"attachment_67939\" style=\"width: 449px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-67939\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-67939 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/bad-per-1.png\" alt=\"bad performance \" width=\"439\" height=\"320\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/bad-per-1.png 439w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/bad-per-1-300x219.png 300w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><p id=\"caption-attachment-67939\" class=\"wp-caption-text\">Lighthouse score<\/p><\/div>\n<p>After adding virtualization-<\/p>\n<div id=\"attachment_67940\" style=\"width: 449px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-67940\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-67940 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/good-performance-1.png\" alt=\"Lighthouse score of massive list with virtualization\" width=\"439\" height=\"320\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/good-performance-1.png 439w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/good-performance-1-300x219.png 300w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><p id=\"caption-attachment-67940\" class=\"wp-caption-text\">Lighthouse score of massive list with virtualization<\/p><\/div>\n<p>&nbsp;<\/p>\n<h3>Setting Up the Project<\/h3>\n<p>To demonstrate virtualization, we\u2019ll create a simple React application using the react-virtualized library.<\/p>\n<p><strong>Step 1:<\/strong> Create a React Application<\/p>\n<pre>npx create-react-app react-virtualization-example\r\n\r\ncd react-virtualization-example\r\n\r\nnpm install react-virtualized<\/pre>\n<p><strong>Step 2:<\/strong> Basic Component Setup<\/p>\n<p>Create a component to display a virtualized list.<\/p>\n<pre>\/\/ src\/VirtualizedList.js\r\n\r\nimport React from 'react';\r\n\r\nimport { List } from 'react-virtualized';\r\n\r\nimport 'react-virtualized\/styles.css'; \/\/ only needs to be imported once\r\n\r\nconst VirtualizedList = ({ items }) =&gt; {\r\n\r\n\u00a0const rowRenderer = ({ key, index, style }) =&gt; (\r\n\r\n\u00a0\u00a0\u00a0&lt;div key={key} style={style} className=\"list-item\"&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0{items[index]}\r\n\r\n\u00a0\u00a0\u00a0&lt;\/div&gt;\r\n\r\n\u00a0);\r\n\r\n\u00a0return (\r\n\r\n\u00a0\u00a0\u00a0&lt;List\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0width={300}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0height={600}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0rowCount={items.length}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0rowHeight={50}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0rowRenderer={rowRenderer}\r\n\r\n\u00a0\u00a0\u00a0\/&gt;\r\n\r\n\u00a0);\r\n\r\n};\r\n\r\nexport default VirtualizedList;<\/pre>\n<p>Integrating the Virtualized Component<\/p>\n<p><strong>Step 3:<\/strong> Use the Virtualized Component in Your App<\/p>\n<pre>\/\/ src\/App.js\r\n\r\nimport React from 'react';\r\n\r\nimport VirtualizedList from '.\/VirtualizedList';\r\n\r\nconst App = () =&gt; {\r\n\r\n\u00a0const items = Array.from({ length: 100000 }, (_, index) =&gt; `Item ${index + 1}`);\r\n\r\n\u00a0return (\r\n\r\n\u00a0\u00a0\u00a0&lt;div className=\"App\"&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;h1&gt;Virtualized List Example&lt;\/h1&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;VirtualizedList items={items} \/&gt;\r\n\r\n\u00a0\u00a0\u00a0&lt;\/div&gt;\r\n\r\n\u00a0);\r\n\r\n};\r\n\r\nexport default App;\r\n\r\n<\/pre>\n<p>Advanced Usage<\/p>\n<p><strong>Step 4:<\/strong> Customizing the Virtualized List<\/p>\n<p>Explore advanced features of react-virtualized such as dynamic row heights, infinite scrolling, and more.<\/p>\n<pre>import React from 'react';\r\n\r\nimport { InfiniteLoader, List, AutoSizer } from 'react-virtualized';\r\n\r\nconst InfiniteVirtualizedList = ({ loadMoreRows, isRowLoaded, rowCount }) =&gt; {\r\n\r\n\u00a0const rowRenderer = ({ key, index, style }) =&gt; (\r\n\r\n\u00a0\u00a0\u00a0&lt;div key={key} style={style} className=\"list-item\"&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0{`Item ${index + 1}`}\r\n\r\n\u00a0\u00a0\u00a0&lt;\/div&gt;\r\n\r\n\u00a0);\r\n\r\n\u00a0return (\r\n\r\n\u00a0\u00a0\u00a0&lt;InfiniteLoader\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0isRowLoaded={isRowLoaded}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0loadMoreRows={loadMoreRows}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0rowCount={rowCount}\r\n\r\n\u00a0\u00a0\u00a0&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0{({ onRowsRendered, registerChild }) =&gt; (\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;AutoSizer&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{({ height, width }) =&gt; (\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;List\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0height={height}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0width={width}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0onRowsRendered={onRowsRendered}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ref={registerChild}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0rowCount={rowCount}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0rowHeight={50}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0rowRenderer={rowRenderer}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/AutoSizer&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0)}\r\n\r\n\u00a0\u00a0\u00a0&lt;\/InfiniteLoader&gt;\r\n\r\n\u00a0);\r\n\r\n};\r\n\r\nexport default InfiniteVirtualizedList;\r\n\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>Virtualization is a powerful technique to enhance the performance of React applications dealing with large data sets. By rendering only the visible items, we can significantly improve the efficiency and responsiveness of our apps. Tools like <a href=\"https:\/\/www.npmjs.com\/package\/react-virtualized\" target=\"_blank\" rel=\"noopener\">react-virtualized<\/a> make implementing this technique straightforward, providing a range of features to cater to various use cases.<\/p>\n<p>Our <a href=\"https:\/\/www.tothenew.com\/react-js-development-consulting-company\">React js competency<\/a> understands the key features, toolchain, and React API libraries to build lightweight and interactive applications. <a href=\"https:\/\/www.tothenew.com\/contact-us\">Reach out<\/a> to our experts to know more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Performance is key in modern web development. Managing efficient rendering becomes crucial as web applications grow in complexity and data volume. One effective technique for improving performance in React applications is virtualization. This article will explore virtualization, why it matters, and how to implement it in a React application. What is Virtualization? In web [&hellip;]<\/p>\n","protected":false},"author":1992,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":34},"categories":[5876],"tags":[55,1308,4064,1504],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/66743"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1992"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=66743"}],"version-history":[{"count":11,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/66743\/revisions"}],"predecessor-version":[{"id":68462,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/66743\/revisions\/68462"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=66743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=66743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=66743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}