2

I use the following code currently -

jQuery(window).on('load', function() {
  // read list of items from the table
  ...
  // getting data the DB
  var connectionstring="Data Source=datasource;Initial Catalog=catalog;User 
  ID=userid;Password=pass;Provider=SQLOLEDB";

  connection.Open(connectionstring);
  var rs = new ActiveXObject("ADODB.Recordset");

  rs.Open("select top 10 from db where id in myitems_list")

... 
// display the result
for (currentRow = 1; currentRow < myitems_list.length + 1; currentRow++) { 
    rs.MoveFirst;
    while(!rs.eof)
        if (jQuery(jQuery(".mytable")[1].rows[currentRow].cells[0]).text().trim()==rs.fields(0)) {
jQuery(jQuery(".mytable")[1].rows[currentRow].cells[1]).html(rs.fields(1));
        ...
    }
});

Here I do the following:

  1. read list of items (there ids) from the first column of the table
  2. for each item found in the table get the data from the database
  3. update the table with the data received from the database (initial order of the items should be unchanged)

It takes a few minutes to update the table. What can I do to speed process of table update? Or, probably, I should move it to separate thread?

12
  • what is 'rs' ? why do you have this statement while(!rs.eof)?
    – karthick
    Feb 14, 2018 at 7:02
  • Assuming the bottleneck isn't slow AJAX, it might be worth using something like React.js for this as the virtual DOM it uses will be a lot faster than updating it with jQuery. Feb 14, 2018 at 7:04
  • @karthick, I've updated the code in the question. But it is not important here.
    – LA_
    Feb 14, 2018 at 7:04
  • For each row in the table you loop through the whole recordset looking for a match? Yes, that sounds expensive and slow.
    – James
    Feb 14, 2018 at 7:06
  • Sounds like your algorithm can be improved before looking for bottlenecks in Javascript. Could also try storing jQuery(".mytable")[1].rows outside of the loop so it's not performing the DOM traversal and lookup every single iteration.
    – Felix Guo
    Feb 14, 2018 at 7:08

3 Answers 3

0

I’m not sure what this pattern is called, but you have two sorted datasets and you want to update one from the other efficiently. You can use an algorithm which loops through each list once.

Sort both lists the same way, and create a counter variable for each list. Compare the ith element of a1 with the jth element of a2. If equal, update a1 from a2 and increment i and j. If less than, increment i. Otherwise increment j.

var a1 = [sorted array];
var a2 = [similarly sorted array];

var i=0, j=0;

while (i < a1.length && j < a2.length) {
  if (a1[i] == a2[j]) {
    // update a1[i]
    i++;
    j++;
  } else if (a1[i] < a2[j]) {
    i++;
  } else {
    j++;
  }
}
1
  • Thanks, James. I am not sure how to sort the recordset (this it the result of SQL database query) and I can not change the sorting order of the table values (jQuery(".mytable")[1]) - the order is important here.
    – LA_
    Feb 14, 2018 at 7:35
0

The problem was with the loop over rs object.

When I changed the code to

// display the result
rs.MoveFirst;
while(!rs.eof) {
    for (currentRow = 1; currentRow < myitems_list.length + 1; currentRow++) { 
        if (jQuery(jQuery(".mytable")[1].rows[currentRow].cells[0]).text().trim()==rs.fields(0)) {
jQuery(jQuery(".mytable")[1].rows[currentRow].cells[1]).html(rs.fields(1));
        ...
        break;
    }
});

it started to work fast.

0

A technique that doesn’t require sorting. Build a hashtable from your existing content that maps the id to the html element, then use the hashtable.

var ht = {};

for (currentRow = 1; currentRow < myitems_list.length + 1; currentRow++) {
        ht[jQuery(".mytable")[1].rows[currentRow].cells[0]).text().trim()] = jQuery(".mytable")[1].rows[currentRow].cells[1];
    }

rs.MoveFirst;
while(!rs.eof) {
  if(ht[rs.fields(0)]) {
    jQuery(ht[rs.fields(0)]).html(rs.fields(1));
  }
  rs.MoveNext();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.