From a93c5c9806f15df120c563ff4fcdcc84c01784b3 Mon Sep 17 00:00:00 2001 From: bezrodnov Date: Tue, 15 Mar 2022 11:31:29 +0100 Subject: [PATCH] Improve row skipping performance (#911) (#912) Co-authored-by: Aleks Bezrodnov --- papaparse.js | 6 +++--- tests/test-cases.js | 29 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/papaparse.js b/papaparse.js index 667a109..b7d3f18 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1163,9 +1163,9 @@ License: MIT if (_config.skipEmptyLines) { - for (var i = 0; i < _results.data.length; i++) - if (testEmptyLine(_results.data[i])) - _results.data.splice(i--, 1); + _results.data = _results.data.filter(function(d) { + return !testEmptyLine(d); + }); } if (needsHeaderRow()) diff --git a/tests/test-cases.js b/tests/test-cases.js index 469c74c..1b0ee33 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1608,6 +1608,31 @@ var PARSE_ASYNC_TESTS = [ data: [['A','B','C'],['X','Y','Z']], errors: [] } + }, + { + description: "File with a few regular and lots of empty lines", + disabled: !FILES_ENABLED, + input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z\n" + new Array(500000).fill(",,").join("\n")], "sample.csv") : false, + config: { + skipEmptyLines: "greedy" + }, + expected: { + data: [['A','B','C'],['X','Y','Z']], + errors: [] + } + }, + { + description: "File with a few regular and lots of empty lines + worker", + disabled: !FILES_ENABLED, + input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z\n" + new Array(500000).fill(",,").join("\n")], "sample.csv") : false, + config: { + worker: true, + skipEmptyLines: "greedy" + }, + expected: { + data: [['A','B','C'],['X','Y','Z']], + errors: [] + } } ]; @@ -1836,9 +1861,9 @@ var UNPARSE_TESTS = [ }, { description: "Returns without rows with no content when skipEmptyLines is 'greedy'", - input: [[null, ' '], [], ['1', '2']], + input: [[null, ' '], [], ['1', '2']].concat(new Array(500000).fill(['', ''])).concat([['3', '4']]), config: {skipEmptyLines: 'greedy'}, - expected: '1,2' + expected: '1,2\r\n3,4' }, { description: "Returns empty rows when empty rows are passed and skipEmptyLines is false with headers",