From 8ba41017470590a614cf513b33f450f08bc07ca3 Mon Sep 17 00:00:00 2001 From: Jari Bakken Date: Mon, 16 Apr 2018 10:02:43 +0200 Subject: [PATCH] Avoid redundant deep cloning when unwinding. --- lib/JSON2CSVBase.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 8d016702..b7d7d05e 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -234,8 +234,12 @@ class JSON2CSVBase { */ unwindData(dataRow, unwindPaths) { return unwindPaths - .reduce((data, unwindPath) => - data.map((dataEl) => { + .reduce((data, unwindPath) => { + function clone(o) { + return unwindPath.indexOf('.') !== -1 ? lodashCloneDeep(o) : Object.assign({}, o); + } + + return data.map((dataEl) => { const unwindArray = lodashGet(dataEl, unwindPath); if (!Array.isArray(unwindArray)) { @@ -244,7 +248,7 @@ class JSON2CSVBase { if (unwindArray.length) { return unwindArray.map((unwindEl, index) => { - const dataCopy = lodashCloneDeep(dataEl); + const dataCopy = clone(dataEl); if (this.opts.unwindBlank && index > 0) { Object.keys(dataCopy).forEach(key => { @@ -254,16 +258,16 @@ class JSON2CSVBase { }) } - lodashSet(dataCopy, unwindPath, unwindEl); return dataCopy; }); } - const dataCopy = lodashCloneDeep(dataEl); + const dataCopy = clone(dataEl); lodashSet(dataCopy, unwindPath, undefined); return dataCopy; - }).reduce((tempData, rows) => tempData.concat(rows), []), + }).reduce((tempData, rows) => tempData.concat(rows), []) + }, [dataRow] ) .reduce((tempData, rows) => tempData.concat(rows), []);