moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.32k stars 730 forks source link

Perf: Memoize digitsRegex #1581

Closed schleyfox closed 6 months ago

schleyfox commented 8 months ago

This is part of a series of PRs based on performance work we have done to improve a use-case involving parsing/formatting hundreds of thousands of dates where luxon was the bottleneck.

Profiles reveal that a substantial portion of fromFormat time is spent in digitsRegex. It is only called from unitsForToken and it is always called with 11 suffixes to match different numbers of digits.

There are 21 numbering systems, so a fully expanded cache would hold 11*21 = 231 regexes.

This is related to https://github.com/moment/luxon/pull/1530 , but involves a much more restricted cache

Benchmark Comparison (name | before | after | after/before):

DateTime.fromFormat | 60,666 ±0.17% | 113,218 ±0.20% | 1.87x
DateTime.fromFormat with zone | 26,687 ±0.18% | 33,563 ±0.19% | 1.26x
linux-foundation-easycla[bot] commented 8 months ago

CLA Signed

The committers listed above are authorized under a signed CLA.

schleyfox commented 8 months ago

/easycla