jquery - How could I speed up this exporting data function using javascript -


i have feature users able export database information. if user choose options export takes 1 minute or more download .csv file. i'm including 1 part of if statement, i'm pulling in data.

here is:

  function exportthedata() {         //get data data array         if(exportvolumedata == 1) {              for(j=0; j<plantdata1.length; j++) {                  = plantdata["mergekey_lvl00"].indexof(plantdata1["mergekey_lvl00"][j]);                  data.push(plantdata["plantname"][i]);                  if(statesexport == 1) {                     countyindex = counties["countyid"].indexof(plantdata["locationid"][i]);                     stateid = counties["stateid"][countyindex];                     statename = states["statename"][states["stateid"].indexof(stateid)];                      data.push(statename);                 }                 if(countyexport == 1) {                     countyindex = counties["countyid"].indexof(plantdata["locationid"][i]);                     countyname = counties["countyname"][countyindex];                      data.push(countyname);                 }                 if(basinsexport == 1) {                     countyindex = counties["countyid"].indexof(plantdata["locationid"][i]);                     subbasinid = counties["subbasinid"][countyindex];                     subbasinindex = basinsub["subbasinid"].indexof(subbasinid);                     basinid = basinsub["basinid"][subbasinindex];                     basinindex = basin["basinid"].indexof(basinid);                     basinname = basin["basinname"][basinindex];                      data.push(basinname);                 }                            if(subbasinsexport == 1) {                     countyindex = counties["countyid"].indexof(plantdata["locationid"][i]);                     subbasinid = counties["subbasinid"][countyindex];                     subbasinindex = basinsub["subbasinid"].indexof(subbasinid);                     subbasinname = basinsub["subbasinname"][subbasinindex];                      data.push(subbasinname);                 }                            if(paddsexport == 1) {                     countyindex = counties["countyid"].indexof(plantdata["locationid"][i]);                     subpaddid = counties["subpaddid"][countyindex];                     subpaddindex = paddsub["subpaddid"].indexof(subpaddid);                     paddid = paddsub["paddid"][subpaddindex];                     paddindex = padd["paddid"].indexof(paddid);                     paddname = padd["paddname"][paddindex];                      data.push(paddname);                 }                            if(subpaddsexport == 1) {                     countyindex = counties["countyid"].indexof(plantdata["locationid"][i]);                     subpaddid = counties["subpaddid"][countyindex];                     subpaddname = paddsub["subpaddname"][paddsub["subpaddid"].indexof(subpaddid)];                      data.push(subpaddname);                 }                            if(fullnameexport == 1) {                     companyindex = getcompanyinfo["mergekey_lvl00"].indexof(plantdata["operatorid"][i]);                     fullname = getcompanyinfo["fullname"][companyindex];                      data.push(fullname);                 }                            if(shortnameexport == 1) {                     companyindex = getcompanyinfo["mergekey_lvl00"].indexof(plantdata["operatorid"][i]);                     shortname = getcompanyinfo["shortname"][companyindex];                      data.push(shortname);                 }                            if(tickerexport == 1) {                     companyindex = getcompanyinfo["mergekey_lvl00"].indexof(plantdata["operatorid"][i]);                     ticker = getcompanyinfo["ticker"][companyindex];                      data.push(ticker);                 }                  volumeindex = plantdata1["mergekey_lvl00"].indexof(plantdata["mergekey_lvl00"][i]);                 startdate = plantdata1["monthstartdate"][volumeindex];                 volumetypeindex = plantdata2["volumetypeid"].indexof(plantdata1["volumetypeid"][j]);                 volumetype = plantdata2["volumetype"][volumetypeindex];                 volumeunit = plantdata2["unit"][volumetypeindex];                 volume = plantdata1["volume"][volumeindex];                  data.push(startdate);                 data.push(volumetype);                 data.push(volumeunit);                 data.push(volume);             }          /* * convert our data csv string */         var csvstring = prepcsvrow(titles, titles.length, '');         csvstring = prepcsvrow(data, titles.length, csvstring);         /* * make csv downloadable*/         var downloadlink = document.createelement("a");         var blob = new blob(["\ufeff", csvstring]);         var url = url.createobjecturl(blob);         downloadlink.href = url;         downloadlink.download = "data.csv";         /** download csv */         document.body.appendchild(downloadlink);         downloadlink.click();         document.body.removechild(downloadlink);    } 

here convert data array csv string:

        function prepcsvrow(arr, columncount, initial) {       var row = ''; // hold data       var delimeter = ','; // data slice separator, in excel it's `;`, in usual csv it's `,`       var newline = '\r\n'; // newline separator csv row     /* * convert [1,2,3,4] [[1,2], [3,4]] while count 2        * @param _arr {array} - actual array split        * @param _count {number} - amount split        * return {array} - splitted array */       function splitarray(_arr, _count) {         var splitted = [];         var result = [];         _arr.foreach(function(item, idx) {           if ((idx + 1) % _count === 0) {             splitted.push('"' + item + '"');             result.push(splitted);             splitted = [];           } else {             splitted.push('"' + item + '"');           }         });         return result;       }       var plainarr = splitarray(arr, columncount);       // don't know how explain       // have follow code       // , understand, it's pretty simple       // converts `['a', 'b', 'c']` `a,b,c` string       plainarr.foreach(function(arritem) {         arritem.foreach(function(item, idx) {           row += item + ((idx + 1) === arritem.length ? '' : delimeter);         });         row += newline;       });       return initial + row;     }  

any idea on how speed up? have on 6,000 rows of data in database. thanks!

changing data.push.. data[data.length] = .. has made function lot faster. also, created variables countyindex , companyindex instead of calling multiple times in same function.

also, cleaning last part of function helped:

  /* * convert our data csv string */  var csvstring = prepcsvrow(titles, titles.length);  csvstring += prepcsvrow(data, titles.length);   /* * make csv downloadable*/  var downloadlink = document.createelement('a'),   blob         = new blob(['\ufeff', csvstring]);   downloadlink.href     = url.createobjecturl(blob);  downloadlink.download = 'data.csv';   /** download csv */  document.body.appendchild(downloadlink);  downloadlink.click();  document.body.removechild(downloadlink); 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -