Skip to content
Advertisement

What is a better approach to searching a large array for a particular entry multiple times on the same page?

I want to display many icons on the same page but I understand looping through the same array many times each render is too much work and it will reduce performance especially in slower devices. How do you approach this problem?

The allIconsSet array is stored and exported in a file and bundled in the app. The services array comes from a server so it can’t have React Components but only strings.

JavaScript

Advertisement

Answer

Simply transform your icon Array into an Object. And you’ll be able to find an icon like this: allIconsSet['smoking'].

JavaScript

If you don’t plan to add additional data, you could ditch the icon field and directly pass the component as the icon property value, like: 'smoking': <FaCigarette />,.

NOTE: Icons exists as webfonts that can be used just like any fonts making them easy to use, customizable with CSS and lightweight. Font Awesome does this, you should check it out, it will certainly be less memory/CPU intensive than having one component per icon.

EDIT: Below you’ll find an example of webfont usage with Google Material Icons.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement