mhdhejazi / CoronaTracker

Coronavirus tracker app for iOS & macOS with maps & charts
https://coronatracker.samabox.com/
GNU General Public License v3.0
1.54k stars 288 forks source link

Parse timeseries header dates and re-use them later #133

Closed rhcpfan closed 4 years ago

rhcpfan commented 4 years ago

Reduce memory usage and increase performance by parsing the timeseries headers from JHU and reuse them later.

rhcpfan commented 4 years ago

I've changed the code to map the header dates into a [String: Date?] where keys are the original headers and the values are the parsed dates.

let dateStrings = headers.dropFirst(4)
let dateValues = dateStrings.map { dateFormatter.date(from: $0) }
let mappedDates = Dictionary(uniqueKeysWithValues: zip(dateStrings, dateValues))

...

for column in confirmedTimeSeries.values.indices {
    let dateString = dateStrings[dateStrings.startIndex + column]
    if let dateEntry = mappedDates[dateString], let date = dateEntry {
        // use date
    }
}
mhdhejazi commented 4 years ago

Merged. Thank you for your contribution!