treatRawData and treatRawStream: Get values more efficiently with Object.values

pull/5/head
eliot-akira 3 years ago
parent 0af674de2c
commit a610cf8cff
  1. 10
      lib/persistence.js

@ -156,7 +156,6 @@ class Persistence {
treatRawData (rawData) { treatRawData (rawData) {
const data = rawData.split('\n') const data = rawData.split('\n')
const dataById = {} const dataById = {}
const tdata = []
const indexes = {} const indexes = {}
// Last line of every data file is usually blank so not really corrupt // Last line of every data file is usually blank so not really corrupt
@ -181,7 +180,7 @@ class Persistence {
corruptItems / data.length > this.corruptAlertThreshold corruptItems / data.length > this.corruptAlertThreshold
) throw new Error(`More than ${Math.floor(100 * this.corruptAlertThreshold)}% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss`) ) throw new Error(`More than ${Math.floor(100 * this.corruptAlertThreshold)}% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss`)
tdata.push(...Object.values(dataById)) const tdata = Object.values(dataById)
return { data: tdata, indexes: indexes } return { data: tdata, indexes: indexes }
} }
@ -192,7 +191,6 @@ class Persistence {
*/ */
treatRawStream (rawStream, cb) { treatRawStream (rawStream, cb) {
const dataById = {} const dataById = {}
const tdata = []
const indexes = {} const indexes = {}
// Last line of every data file is usually blank so not really corrupt // Last line of every data file is usually blank so not really corrupt
@ -224,11 +222,9 @@ class Persistence {
return return
} }
Object.keys(dataById).forEach(function (k) { const data = Object.values(dataById)
tdata.push(dataById[k])
})
cb(null, { data: tdata, indexes: indexes }) cb(null, { data, indexes: indexes })
}) })
lineStream.on('error', function (err) { lineStream.on('error', function (err) {

Loading…
Cancel
Save