DEV Community

Cover image for Flashdate – a drop-in for Python's dateparser, 100x faster
Shrinish Vhanbatte
Shrinish Vhanbatte

Posted on

Flashdate – a drop-in for Python's dateparser, 100x faster

dateparser reads dates the way people write them: "March 5, 2024", "le 5 mars 2024", "3 days ago". It is downloaded about 45 million times a month, and it is slow. One ISO date takes it about 670 microseconds. A scraper that reads ten million dates spends about two hours on it.

flashdate is a modified copy of dateparser with the same API and the same results. It reads common date formats 100 to 250 times faster, and it is still pure Python.

Making it fast was the easier half. The harder half was knowing that it still gives the same answer for every string. This article covers both.

Why dateparser is slow

dateparser does not know the language of a string in advance. For every string it works out which of its 205 languages could apply, translates the words to English, splits the result into tokens, and tries several parsers in turn.

An ISO date like 2024-03-05 goes through all of these steps, although nothing in it needs translating.

What flashdate does differently

Three changes give most of the speed.

  • Direct routes for common shapes. ISO dates, numeric dates like 03/05/2024, and dates with month names in English and six other languages are recognised by their shape and read straight away. Anything else goes through the full steps, as before. On dateparser's own evaluation set of 300,000 article dates, 80% take a direct route.
  • Languages are skipped early. A language is skipped when the string contains a letter or a word that the language cannot have.
  • Work is done once. The list of languages is built once, not for every date. Time zones are searched once per string. Known words are matched with a prefix tree.

Two columns. dateparser: the string 2024-03-05 goes through four steps (find which of 205 languages could fit, translate the words to English, split the text into tokens, try several parsers in turn) and takes 672 microseconds. flashdate: it checks whether the string has a known shape, reads it directly, and takes 4.8 microseconds. Any other string takes dateparser's full steps, as before.
Figure 1. The route one ISO date takes through each library.

Chart of microseconds for one date, on a logarithmic scale, for seven kinds of string. dateparser sits between 391 and 7,443 microseconds. flashdate sits between 4.8 and 432. The speedups are 141x, 259x, 129x, 131x, 156x, 9x and 17x.
Figure 2. Time to read one date, on a standard GitHub Actions runner.

The numbers behind the chart

Microseconds per date string:

Date string dateparser flashdate Speedup
2024-03-05 672 4.8 141x
2024-03-05T13:45:00Z 1,388 5.4 259x
03/05/2024 679 5.3 129x
March 5, 2024 691 5.3 131x
le 5 mars 2024 935 6.0 156x
3 days ago 391 41.7 9x
Text that is not a date 7,443 432 17x

These numbers come from a public run. Anyone can open it or start it again in a fork. The times change with the machine. The speedup changes much less: across three runners, the common formats were 103 to 259 times faster.

How I know it is the same

I never decided what the right answer for a string is. The untouched dateparser is the judge. The only question is whether both libraries give the same answer.

Diagram. One string goes to both libraries: dateparser, untouched, and flashdate, with the change. The answers are compared: same date, same error? If they are the same, the change stays. If they differ, it is fixed or thrown out. What went through this check: 112 million strings, 127 groups of strings, 31 combinations of settings, 17 clocks and time zones, 0 differences.
Figure 3. How every change was checked.

  • 112 million strings, in 127 groups, were parsed by both. That includes every valid ISO date from year 1 to 9999 and every valid numeric date in both day and month orders. There were 0 differences.
  • 31 combinations of settings, called in mixed order, gave 836,052 results. Mixed order matters: it catches a cache that leaks from one call into the next.
  • 17 clock and time zone setups, because "3 days ago" depends on what time it is now.
  • Errors too. Where dateparser raises an exception, flashdate raises the same one.
  • dateparser's own tests, more than 25,000 of them, pass against the built package on Python 3.10 to 3.14.

A check is only worth something if it can fail, and this one did. An early version had a faulty cache, and the comparison caught it.

It also caught a case where flashdate was arguably more correct. On a machine set to German or Japanese, it read "1:45 PM" where dateparser does not. I changed flashdate to match, because code that switches to it should not start behaving differently.

What it costs

Memory, in one case. Usually flashdate uses as much memory as dateparser, to within 1 MB, or less. When dates in all 205 languages go through one process, it keeps 68 MB where dateparser keeps 55 MB. The difference is fixed and does not grow. The table is in the README, and reducing it is planned for the next release.

Bar chart of megabytes kept after parsing, dateparser against flashdate. Only the import: 5.5 and 5.8. 10,000 ISO and numeric dates: 8.1 and 8.7. 10,000 French, Spanish and Russian dates: 9.8 and 6.0. 50,000 dates of every kind: 42.4 and 41.9. 10,000 dates in all 205 languages: 54.6 and 67.6, which is 13 MB more.
Figure 4. Memory kept after parsing.

How it was built

Most of the code was written with Claude Code, an AI coding tool, and the commits say so. I chose the target, set the rules and made the decisions. The agent wrote, measured and checked.

One rule made this workable: nothing is believed, everything is measured. That applied to the agent's claims, to the reviewer's claims, and to mine.

Four rows, each with what was said, what the measurement said, and what happened. On my laptop, 10 times faster for relative dates and 19 for non-dates: public runners gave 8 to 10 times and 17 times, so the numbers were corrected. The reviewer said unusual time offsets are slower than before: 494 microseconds against 2,300 in dateparser, so the claim was dropped. My question, is the speed paid for with memory: the same within 1 MB in nine cases and 13 MB more in one, published with a fix planned. The agent said a new test covers the fix: undo the fix and the test has to fail, and every new test was seen failing.
Figure 5. What was said, and what was measured.

  • A change stays only if dateparser agrees. Every change was run against the untouched dateparser, on every string, before it was kept.
  • A test must be seen failing. Each new test was checked by undoing the fix it covers and watching the test fail. A test that cannot fail proves nothing.
  • The code was reviewed, and the review was measured too. Before the release, a separate AI session reviewed the whole change without knowing how it was written. It reported 8 issues, none of which gave a wrong date. Seven were fixed. The eighth was a claim about speed that did not hold when measured, so nothing was changed.
  • Published numbers were measured again in public. The first benchmark ran on my laptop. Moving it to a public GitHub run showed that two claims were slightly high, and I corrected them: "10 times" became "8 to 10 times", and "19 times" became "17 times".
  • Costs were measured, not argued. When I asked whether the speed was bought with memory, the answer came from a benchmark. It found one case that costs 13 MB more, and that case is now in the README.

Agentic coding makes writing code cheap. It does not make the code right. What makes it right is a check that does not depend on anyone's word, including the agent's.

What I would repeat

  • Keep the old system as the judge. It removes every argument about what is correct.
  • Make sure the check can fail. Break something on purpose and watch the check catch it.
  • Publish numbers that others can run again. A benchmark on a laptop cannot be checked. A public run can.

Try it

pip install flashdate
Enter fullscreen mode Exit fullscreen mode
import flashdate

flashdate.parse("le 5 mars 2024")
# datetime.datetime(2024, 3, 5, 0, 0)
Enter fullscreen mode Exit fullscreen mode

Moving an existing codebase

It takes one changed line per file. Change the import and leave the rest of the code alone:

# before
import dateparser

# after
import flashdate as dateparser
Enter fullscreen mode Exit fullscreen mode

Every call below that line stays as it is: dateparser.parse(...), the settings, the languages, DateDataParser. Where you import from a submodule, only the package name changes:

from flashdate.search import search_dates
from flashdate.date import DateDataParser
Enter fullscreen mode Exit fullscreen mode

You do not have to take my word for the results. Check them on your own data first:

  1. Install flashdate. It does not replace dateparser, so nothing changes yet.
  2. Run both on a sample of your own dates and compare.
  3. Switch the import. To go back, change that one line back.
import dateparser
import flashdate

for text in my_dates:
    assert flashdate.parse(text) == dateparser.parse(text), text
Enter fullscreen mode Exit fullscreen mode

Relative dates such as "3 days ago" depend on the current time. To compare those, pass the same RELATIVE_BASE setting to both.

It installs next to dateparser under its own name, so you can run both and compare. If you find a string where they differ, that is a bug: please open an issue.

The design, the language data and the tests are the work of dateparser's maintainers. flashdate keeps their licence and only changes how fast the answers come. Code, benchmarks and the comparison scripts:

GitHub logo shrinish123 / flashdate

A faster dateparser: same API and results, common date formats read in microseconds

flashdate

A faster dateparser. Same API, same results, and the date formats that most data is written in are read in a few microseconds instead of a few hundred.

import flashdate as dateparser
dateparser.parse("2024-03-05T13:45:00Z")
dateparser.parse("March 5, 2024 1:45 PM")
dateparser.parse("le 5 mars 2024")
dateparser.parse("3 days ago")
Enter fullscreen mode Exit fullscreen mode

flashdate is an independent, modified copy of dateparser 1.5.1. It is not made, endorsed or supported by the maintainers of dateparser: please report problems with it here, not to them.

Install

pip install flashdate

It installs next to dateparser, under its own import name, so you can run both in one program and compare them. Python 3.10 or later.

How much faster

Microseconds per date string, one thread, every string different, with CPython 3.12 on a standard GitHub Actions runner (Ubuntu, Intel Xeon…

Top comments (4)

Collapse
 
gautamtalksdev profile image
Gautam K •

Same API and the same results is the promise that matters for a drop-in. How do you check the same results part, running both against a large corpus and diffing? That differential test would be the most convincing line in the README.

Collapse
 
shrinish123 profile image
Shrinish Vhanbatte •

Yes @gautamtalksdev, a differential test against the unmodified dateparser, on every release.

the release workflow on GitHub runs the comparison against the built package and fails to publish if a diff is found. That is every tenth string of the corpus under 31 combinations of settings, 65,952 outcomes, plus dateparser's own 25,725 tests on each of Python 3.10 to 3.14.

Before the 1st release, I also stress-tested it on 112 million strings in the fast-path formats, 836,052 results from the full corpus, and dateparser's 248,950 article dates to find 0 differences.

Each comparison checks the date, the time zone, the detected language and the error raised. The README has it under "How it is the same": github.com/shrinish123/flashdate#h.... The method, the script and the limits of the checks are in github.com/shrinish123/flashdate/b..., and the script runs on your own machine in a few minutes.

Collapse
 
userxnom profile image
Abdullah Naseer •

The speed is impressive, but the part I respect most is the method: keeping untouched dateparser as the judge and running 112 million strings through both is the only way a claim like this earns trust. Direct routes for common shapes make sense too — most real-world dates are ISO or a handful of familiar formats, so skipping the 205-language detection for those is a big win. Publishing the benchmark as a public GitHub run instead of laptop numbers is the right call as well. Which shapes still fall back to the slow path in practice, and is that the memory case you flagged in the README, or a different trade-off?

Collapse
 
shrinish123 profile image
Shrinish Vhanbatte •

Thanks @userxnom Shapes that still take the longer route are : two-digit years,dates with a time or a weekday in languages other than English, and free-form phrases, and yes even the extra memory cost comes up in these cases itself, extra memory is per-language patterns used to skip a language early, 3.2 MB of pattern analysis and 2.5 MB of caches.It shows only when one process loads all 205 languages, and it does not grow with the number of strings.

Reducing it is planned for the next release.