title: Parsing large CSV file in LWC
body: <p>I have implemented the following code to parse a csv file, convert to a JSON array and send the JSON result to apex controller, which invokes the batch class to  process the DML operation for opportunityLineItem object. The code is working fine up to a maximum of 4000 rows (files have 22 columns with values). When there are 5000 records, the process throws an error and stops (it does not call the apex server). Why does it stop if there are 4000 records? Is there any limit for parsing the csv records in LWC?</p>
  <p>Code:</p>
  <pre><code>if (!this.csvFile) {
        console.log(&quot;file not found&quot;);
        return;
      }
      this.showprogressbar = true;
      let reader = new FileReader();
      let ctx = this; // to control 'this' property in an event handler
      reader.readAsText(this.csvFile, &quot;Shift_JIS&quot;);
      reader.onload = function (evt) {
        console.log('reader:'+evt.target.result);
        let payload = ctx.CSV2JSON(evt.target.result, ctx.CSVToArray);
        let json = null;
        let error = null;
        console.log(&quot;payload:&quot; + payload);
        setJSON({
          payload: payload,
          crud: crud,
          csvFile:ctx.csvFile
        })
          .then((result) =&gt; {
            json = result;
            var err = json.includes(&quot;Error&quot;);
            console.log('err====='+err);
            if(err)
            {
              console.log('json==###=='+json);
                ctx.error = json;
                console.log(&quot;error:&quot;+ctx.error);
                ///s/ alert('error');
                
                ctx.showloader=false;
                ctx.hasError=true;
            }
              else{
              ctx.jobinfo(json);
              console.log(&quot;apex call setJSON() ===&gt; success: &quot; + json);
              //ctx.error = undefined;
            }
          })
          .catch((error) =&gt; {
            error =ctx.error;
            console.log(&quot;error:&quot; + error.errorCode + ', message ' + error.message);
            ///s/ alert('error');
            
            ctx.showloader=false;
            ctx.hasError=true;
  
     
            if (error &amp;&amp; error.message) {
              json = &quot;{'no':1,'status':'Error', 'msg':'&quot; + error.message + &quot;'}&quot;;
            } else {
              json = &quot;{'no':1,'status':'Error','msg':'Unknown error'}&quot;;
            }
            
          });
      };
      reader.onerror = function (evt) {
        /*ctx.mycolumns = [
          { label: &quot;no&quot;, fieldName: &quot;no&quot;, type: &quot;number&quot;, initialWidth: 50 },
          {
            label: &quot;status&quot;,
            fieldName: &quot;status&quot;,
            type: &quot;text&quot;,
            initialWidth: 100
          },
          { label: &quot;message&quot;, fieldName: &quot;msg&quot;, type: &quot;text&quot; }
        ];
        ctx.mydata = [{ no: &quot;1&quot;, status: &quot;Error&quot;, msg: &quot;error reading file&quot; }];
              */
        //$A.util.toggleClass(spinner, &quot;slds-hide&quot;); // hide spinner
      };
          //  ctx.showloader=false;
      console.log(&quot;mydata:===&quot; + ctx.mydata);
      alert('onerror');
      
    }
          
          
  
    CSV2JSON(csv, csv2array) {
      let array = csv2array(csv);
      //console.log(&quot;csv:::&quot;+csv);
      //console.log(&quot;csv2array:&quot;+csv2array);
      let objArray = [];
      //console.log(&quot;objArray:&quot;+objArray);
      var headervar = oppheader;//'Name,BillingCity,Type,Industry';
      console.log('headervar:::'+headervar);
      let headerArray = headervar.split(',');
      for (let i = 1; i &lt; array.length; i++) {
        objArray[i - 1] = {};
        /*for (let k = 0; k &lt; array[0].length &amp;&amp; k &lt; array[i].length; k++) {
          let key = array[0][k];
          if(key === 'DW予定日')
  
          elseif(key === 'DW予定日')
  
          elseif(key === 'DW予定日')
          
          console.log(&quot;key:&quot;+key);
          this.hearder=key;
          objArray[i - 1][key] = array[i][k];
        }*/
        for (let k = 0; k &lt; headerArray.length; k++) {
          let key = headerArray[k];
          console.log(&quot;key====:&quot;+key);
          this.hearder=key;
          objArray[i - 1][key] = array[i][k];
        }
      }
      objArray.pop();
      //console.log(&quot;objArray:===&quot;+objArray.length);
      this.rowCount = objArray.length;
      //console.log(&quot;rowCount+++++++&quot; + this.rowCount);
      let json = JSON.stringify(objArray);
      //console.log(&quot;json:===&quot;+json.length);
      let str = json.replace(&quot;/},/g&quot;, &quot;},\r\n&quot;);
      //console.log(&quot;str:=======&quot;+str);
      return str;
    }
  
    CSVToArray(strData, strDelimiter) {
      console.log('CSVToArray');
      // Check to see if the delimiter is defined. If not,
      // then default to comma.
      //console.log('strData:'+strData);
      //console.log(&quot;strDelimiter::&quot; + strDelimiter);
      strDelimiter = strDelimiter || &quot;,&quot;;
      //console.log(&quot;strDelimiter:&quot; + strDelimiter);
      // Create a regular expression to parse the CSV values.
      var objPattern = new RegExp(
        // Delimiters.
        &quot;(\\&quot; +
          strDelimiter +
          &quot;|\\r?\\n|\\r|^)&quot; +
          // Quoted fields.
          '(?:&quot;([^&quot;]*(?:&quot;&quot;[^&quot;]*)*)&quot;|' +
          // Standard fields.
          '([^&quot;\\' +
          strDelimiter +
          &quot;\\r\\n]*))&quot;,
        &quot;gi&quot;
      );
      // Create an array to hold our data. Give the array
      // a default empty first row.
     // console.log(&quot;objPattern:&quot; + objPattern);
      var arrData = [[]];
      // Create an array to hold our individual pattern
      // matching groups.
     // console.log(&quot;arrData:&quot; + arrData);
      var arrMatches = null;
      // Keep looping over the regular expression matches
      // until we can no longer find a match.
      while ((arrMatches = objPattern.exec(strData))) {
        // Get the delimiter that was found.
        var strMatchedDelimiter = arrMatches[1];
        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        if (strMatchedDelimiter.length &amp;&amp; strMatchedDelimiter != strDelimiter) {
          // Since we have reached a new row of data,
          // add an empty row to our data array.
          arrData.push([]);
        }
        // Now that we have our delimiter out of the way,
        // let's check to see which kind of value we
        // captured (quoted or unquoted).
        if (arrMatches[2]) {
          // We found a quoted value. When we capture
          // this value, unescape any double quotes.
          var strMatchedValue = arrMatches[2].replace(new RegExp('&quot;&quot;', &quot;g&quot;), '&quot;');
        } else {
          // We found a non-quoted value.
          var strMatchedValue = arrMatches[3];
        }
        // Now that we have our value string, let's add
        // it to the data array.
        arrData[arrData.length - 1].push(strMatchedValue);
      }
      // Return the parsed data.
      return arrData;
    }
  </code></pre>