I have an array with 1000 items that are complex objects.
I need to loop through the array to run some function, when the array is small like 100 products, the performance seems to be alright.
But when it reaches 400 or above products, the performance goes down significantly on the browser.
So I wanted to do something like first loop through top 100 products, then continue loop the next 100 products subsequently.
Not sure is that a good idea? Is there any better way to handle looping through a huge array?
Code Example
The actual case is to loop through selected 1000 of checkboxes.
I have a function to append li
dynamically to a DOM
.
populateEnrichedProductList = (id, name) => {
let html =
`<li class="list-group-item product-item-name" productName="${name}" productId="${id}" data-desc="" data-ingredient="" data-allergens="">` +
`${name}` +
`<span class="error"><i class="fas fa-exclamation-triangle"></i></span>` +
'<div class="spinner">' +
'<div class="spinner-border text-warning" role="status">' +
'<span class="sr-only">Loading...</span>' +
'</div >' +
'</div>' +
'</li >';
$('#selectedProductList').removeClass('hidden');
$('#productListUL').addClass('hidden');
$('#selectedProductList').append(html);
};
Here I loop through the checked items, and run the function populateEnrichedProductList
for each item of the array.
let selectedProducts = '#selectProductForm input:checked';
$(selectedProducts).each(function(){
let pid = $(this).attr('id'),
pname = $(this).attr('name');
populateEnrichedProductList(pid, pname);
});
As mentioned, there were no issues when array is small, but performance dropped when array has more data.
It would be great if anyone can show an example of better handling it to improve the performance.