Optimizing Javascript DateTime formatting (50x perf)
Published in
1 min readDec 27, 2017
Formatting dates and times in javascript can be very expensive in terms of CPU cost. Take this simple piece of code that formats a string value like "2017-12-27T16:37:37.417Z" to 8:37 AM
The conversion toLocaleTimeString take a lot of time. We can improve performance by a factor of ~50x by replacing with:
When running both functions 10,000 times asTimeOld takes 4300ms, asTimeNew takes 95ms.
Note that we create the Intl.DateTimeFormat type globally then reuse it in the asTimeNew function.