Try to avoid loops in JavaScript for better performance

Use objects instead of arrays

A benefit of objects is that you can directly call its children elements (properties) by name. While arrays only have index numbers.

Here is a simple example:

const exampleDataArray = [ { id: 8462943, name: "Google", url: "http://google.com" }, { id: 9847323, name: "Amazon", url: "http://amazon.com" }, { id: 938442934, name: "Twitter", url: "http://twitter.com" } ];

function getDataById(id) { for (let i = 0; i < exampleDataArray.length; i++) { if (exampleDataArray[i].id === id) { return exampleDataArray[i]; } } }

So in this example when I want to get an item by its ID or any other property I have to go through each other item first. Depending on how many items you have and how often you need to get one item, it can take lot of time.

If you use an object instead of an array it could look like this:

const exampleDataObject = { 8462943: { name: "Google", url: "http://google.com" }, 9847323: { name: "Amazon", url: "http://amazon.com" }, 938442934: { name: "Twitter", url: "http://twitter.com" } };

function getDataById(id) { return exampleDataObject[id]; }

This way we can directly point to the item we want and don't have to use a loop.