var dateTimeISO = '2024-08-21T17:00:00.000Z'
var normalizeToLocalDate = (date) =>
date.length === 10 && /^\d{4}-\d{2}-\d{2}$/.test(date) ? ` ${date}` : date;
var parseDateISO = (date) => new Date(date);
var parseDate = (date) => new Date(normalizeToLocalDate(date));
var parseDateInline = (date) =>
date.length === 10 && /^\d{4}-\d{2}-\d{2}$/.test(date) ?
new Date(` ${date}`) :
new Date(date);
new Date(dateTimeISO)
parseDateISO(dateTimeISO)
parseDate(dateTimeISO)
parseDateInline(dateTimeISO)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Date | |
parseDateISO | |
parseDate | |
parseDateInline |
Test name | Executions per second |
---|---|
new Date | 4364059.5 Ops/sec |
parseDateISO | 3776009.2 Ops/sec |
parseDate | 3387259.2 Ops/sec |
parseDateInline | 3798111.5 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of three different approaches for parsing ISO dates in JavaScript.
What's being tested?
The benchmark tests the execution time of each approach on parsing an ISO date string (dateTimeISO
) into a Date object:
new Date(dateTimeISO)
: This is the built-in JavaScript method for parsing a date string.parseDateISO(dateTimeISO)
: This function uses the new Date()
constructor to parse the date string.parseDate(dateTimeISO)
: This function first normalizes the date string using a custom function (normalizeToLocalDate
) and then passes it to the built-in new Date()
constructor.parseDateInline(dateTimeISO)
: This function uses a custom expression to check if the date string is in the correct format (10 characters long with a specific format) and then parses it using the new Date()
constructor.Options comparison
The benchmark compares the performance of these four approaches:
new Date()
method:parseDateISO()
function:new Date()
constructor, which is fast and efficient.parseDate()
function:parseDateISO()
.parseDateISO()
due to the overhead of the custom functions.parseDateInline()
function:parseDate()
's input validation and normalization with the efficiency of new Date()
constructor.Library usage
There is no explicit library usage in this benchmark. However, the normalizeToLocalDate
function used in the parseDate()
approach likely relies on built-in JavaScript functions or libraries for string manipulation and date parsing.
Special JS feature/syntax
This benchmark does not use any special JavaScript features or syntax that would require specific knowledge of those features to understand the benchmark setup. The focus is on comparing the performance of different approaches, making it accessible to a wide range of software engineers.
Alternatives
Other alternatives for parsing ISO dates in JavaScript might include:
However, the built-in new Date()
method remains a widely supported and efficient way to parse ISO dates in JavaScript.