0

Using the below piece of logic we are trying to loop the records which are in DB suppose if I have 1000 rows then it is getting iterated 1000 times where it is taking more time instead of doing this how can I make it simpler and achieve the results quickly

 for (var row in db) {
            if (db.hasOwnProperty(row)) {
                if (currentid == db[row].actualid)
                    optionsHtmlString += '<option value="' + db[row].MemberId + '" selected="selected" title="' + db[row].FullName + '">' + db[row].FullName + '</option>';
                else
                    optionsHtmlString += '<option value="' + db[row].MemberId + '" title="' + db[row].FullName + '">' + db[row].FullName + '</option>';
            }
        }
  • 1
    Well, you can make it simpler by using `for (var row of db) { ... }` without the `hasOwnProperty` check. I would say that using a `select` with 1000s of rows is not the greatest user experience... – Heretic Monkey Nov 25 '20 at 14:52
  • 1
    You could potentially use `map()` to build the strings to be appended, but however you approach this you're not going to see much benefit as the logic flow is the same, ie. loop, concatenate, append. If you're seeing performance issues with 1000 options. Then I'd suggest you instead move the logic to the server side, using an AJAX powered auto-complete field. – Rory McCrossan Nov 25 '20 at 14:55
  • You could build the HTML server-side (eg in a partial view using ASP.Net-MVC) which will generally be quicker, depending on the server specs of course. – freedomn-m Nov 25 '20 at 14:56
  • hasownproperty check is mandatory – deepa123 menon Nov 25 '20 at 15:03
  • it should be handled client side can i use foreach – deepa123 menon Nov 25 '20 at 15:04
  • Like @RoryMcCrossan suggested, you better choose an auto-complete method as an alternative. Because even if you optimize your JS code, or generate it (hard-coded DOM) via your server, the browser will try to render a long list of options and the user will get lost among them :) If the DB data is not frequently updated, and the query is the same (not different combinations of filters or parameters) , you could store it in a static json.. And use it with Ajax autocomplete. Less extra DB queries too ;) – Bilel Nov 25 '20 at 15:05
  • how can i implement that – deepa123 menon Nov 25 '20 at 15:06
  • I can't imagine that a user wants to search trough 1000 entries. Why don't you make it dynamically. Just return first 100 results if no search character is set. On typing in the search field do a new db request and update the results – Thallius Nov 25 '20 at 15:08
  • but it is not smart search it is a drop down which needs to fetch all the members – deepa123 menon Nov 25 '20 at 15:09
  • can i use a foreach loop instead of for oop if i use foreach then how can i validate db.hasownproperty(true) condition – deepa123 menon Nov 25 '20 at 15:15
  • 1
    *"it is not smart search it is a drop down"* - the suggestions are to *make* it a "smart search" – freedomn-m Nov 25 '20 at 15:23
  • Any other type of `for` (`for(var i;i – freedomn-m Nov 25 '20 at 15:28
  • Take a look at select2.org. this might be what you want to have – Thallius Nov 25 '20 at 15:59
  • could any one please help me with sample logic to achieve this performance issue – deepa123 menon Nov 25 '20 at 16:38
  • No, because your concept is wrong. But you won’t accept this event if 100 people will tell you. – Thallius Nov 25 '20 at 17:14

0 Answers0