<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/locale/ja.js"></script>
(new Date('2023-06-12T17:05:31.000000Z')).toLocaleDateString('ja-JP', {year:'numeric', month:'2-digit', day:'2-digit'}).replaceAll('/', '')
(new Date('2023-06-12T17:05:31.000000Z')).toLocaleDateString('ja-JP', {year:'numeric', month:'2-digit', day:'2-digit'}).replace('/\//g', '')
dayjs('2023-06-12T17:05:31.000000Z').locale('ja').format('YYYYMMDD')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native replaceAll | |
native replace | |
dayjs |
Test name | Executions per second |
---|---|
native replaceAll | 20630.7 Ops/sec |
native replace | 20386.4 Ops/sec |
dayjs | 357811.8 Ops/sec |
Let's dive into the explanation of the provided benchmark.
What is being tested?
The benchmark measures the performance difference between using native JavaScript methods and a library (dayjs) to format dates in Japanese locale.
Options compared:
There are three test cases:
replaceAll
: This method uses the built-in replaceAll()
method of strings in JavaScript, which replaces all occurrences of a substring with another substring.replace
: This method uses the built-in replace()
method of strings in JavaScript, which replaces all occurrences of a pattern with a replacement string.Pros and Cons:
replaceAll
:replace
:replaceAll
, as it allows for regular expressions.replaceAll
if used with a simple pattern.replaceAll
.Library: dayjs
dayjs is a popular JavaScript library that provides a more convenient and feature-rich way to work with dates. It's designed to be fast and efficient while providing a simple API for formatting dates in various locales.
In the provided benchmark, dayjs is used to format the date string using the format()
method, which takes a locale and a format string as arguments.
Special JS feature/syntax
There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. The focus is on comparing the performance of different methods for formatting dates in Japanese locale.
Alternatives
If you're looking for alternative libraries or approaches, here are a few options:
String.prototype.replace()
method: If you prefer to avoid using external libraries, the native replace()
method can be used instead of dayjs.Keep in mind that these alternatives may have different performance characteristics and usage patterns compared to dayjs.