var dateStrings = Array(10000).fill(null).map(() => (new Date()).toISOString());
var padDate = date => (date < 10 ? `0${date}` : date);
dateStrings.map((d) => {
const date = new Date(d);
return `${padDate(date.getDate())}.${padDate(date.getMonth() + 1)}.${padDate(date.getYear())}`;
});
dateStrings.map((d) => {
const [date, time] = d.split('T')
const [year, month, day] = date.split('-');
return `${day}.${month}.${year}`;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Parse Date | |
String split |
Test name | Executions per second |
---|---|
Parse Date | 85.4 Ops/sec |
String split | 127.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
The provided benchmark is designed to compare the performance of two approaches for processing date strings:
T
separator in ISO 8601 format.Comparison of options
There are two main approaches being compared:
Pros and Cons
Here are some pros and cons of each approach:
Library and purpose
The padDate
function is a custom utility function that pads the day, month, and year values with leading zeros if necessary. This is used in both benchmark definitions to ensure consistent output.
Special JS feature or syntax
There is no special JavaScript feature or syntax being used in this benchmark.
Alternatives
If you're looking for alternative approaches, here are a few options:
Overall, the choice of approach depends on your specific requirements and priorities. If you need high performance and are willing to sacrifice readability, the String Split approach may be suitable. Otherwise, the Date Parsing approach may provide better performance and maintainability.