1

I am doing a node app where I came across a scenario where If I have an array of 1000 elements and pass it from Node - Express (Backend) to ejs(Only HTMl and CSS Bootstrap) page which is a front end and loop through it , it takes lot of time to display the elements.Can you suggest a way to efficiently display the items from the array and display the items.

 <% ordersArray.reverse().forEach(function(values){ %>
<div class="card" style="width: 18rem;">
  <img class="card-img-top" src="..." alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title"><%= values.firstName %></h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary">Go somewhere</a>
  </div>
</div>
<% }) %>

In the above example ordersArray contains 1000-2000 elements.

inside the user Schema I have a fields called as posts which is stores the post in an array of JSON for example.[{......},{.......},{........},{. .........}].Now how do I limit this and get only few records and keep a track of it.

ashwanth s
  • 105
  • 1
  • 1
  • 10
  • Why not send them through pagination? – gaganshera May 21 '20 at 10:03
  • @gaganshera Can you explain more clearly in detail? Because pagination will help to limit the no of contents to display what if I want all the datas to get displayed?For example like posts that get displayed in facebook? – ashwanth s May 21 '20 at 10:08
  • Facebook limits the number of records it fetches at a time. So for example if you wanna implement an infinite scroll like fb does, you just bring in your first 20-50 records that you wanna show when the page loads. Then when the user reaches the bottom of the page (or a before he reaches the bottom), you can fetch the next 50 records and show them beneath your previous 50, and so on. – gaganshera May 21 '20 at 10:13
  • @gaganshera In my case I query only by Id .But inside the user Schema I have a fields called as posts which is stores the post in a array of JSON for example.[{......},{.......},{........},{. .........}].Now how do I limit this and get only few records.Is there a way to limit is it in the font side rather than the server side? – ashwanth s May 21 '20 at 10:20
  • First check which part is the one that's actually taking time. Is it the server call, or is it the redering of the 1000 records? Based on that, you can decide if you need to optimize server side or the client side. – gaganshera May 21 '20 at 10:29
  • @gaganshera I find that from server side it is slowing down due to two for loops .Is there a way to optimize that.My for loop works a follows i=3 and j=15.FOr Each 'i' it iterates 15 times.Thats the reason it is very slow.Is there a way to optimize this for loop – ashwanth s May 21 '20 at 10:40

0 Answers0