Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Intl.DateTimeFormat formatToParts vs. format then regex
(version: 0)
Comparing performance of:
hackyOffset vs partsOffset vs dtf.formatToParts vs dtf.format
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
jsDate = new Date() dtf = new Intl.DateTimeFormat("en-US", { hour12: false, timeZone: "America/New_York", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", era: "short", }) // from luxon function hackyOffset(dtf, date) { const formatted = dtf.format(date).replace(/\u200E/g, ""), parsed = /(\d+)\/(\d+)\/(\d+) (AD|BC),? (\d+):(\d+):(\d+)/.exec(formatted), [, fMonth, fDay, fYear, fadOrBc, fHour, fMinute, fSecond] = parsed ?? [] return [fYear, fMonth, fDay, fadOrBc, fHour, fMinute, fSecond] } const typeToPos = { year: 0, month: 1, day: 2, era: 3, hour: 4, minute: 5, second: 6, } function partsOffset(dtf, date) { const formatted = dtf.formatToParts(date) const filled = [] for (let i = 0; i < formatted.length; i++) { const { type, value } = formatted[i] const pos = typeToPos[type] if (type === "era") { filled[pos] = value } else if (pos !== undefined) { filled[pos] = parseInt(value, 10) } } return filled }
Tests:
hackyOffset
hackyOffset(dtf, jsDate)
partsOffset
partsOffset(dtf, jsDate)
dtf.formatToParts
dtf.formatToParts(jsDate)
dtf.format
dtf.format(jsDate)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
hackyOffset
partsOffset
dtf.formatToParts
dtf.format
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
hackyOffset
521894.5 Ops/sec
partsOffset
251861.4 Ops/sec
dtf.formatToParts
402347.7 Ops/sec
dtf.format
583438.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Overview** The benchmark measures the performance of four different approaches: 1. `dtf.formatToParts(jsDate)` 2. `dtf.format(jsDate)` followed by a regular expression replacement 3. `hackyOffset(dtf, jsDate)` These approaches are used to format and parse dates using the `Intl.DateTimeFormat` API. **Internationalized Date Time Format** The `Intl.DateTimeFormat` API is used to format dates in a locale-specific way. The `formatToParts()` method returns an array of objects containing the formatted date parts, while the `format()` method returns a string representing the formatted date. **Benchmark Definition JSON** The benchmark definition JSON contains the following information: * Four test cases: `hackyOffset`, `partsOffset`, `dtf.formatToParts`, and `dtf.format` * The `Name` field specifies the name of each test case * The `Description` field is empty, indicating that no description is provided for these tests * The `Script Preparation Code` field contains the code necessary to prepare the environment for each test * The `Html Preparation Code` field is empty, indicating that no HTML preparation code is needed **Library: Luxon** The `hackyOffset()` function is part of the Luxon library, which provides a modern and efficient way to work with dates in JavaScript. The function takes an `Intl.DateTimeFormat` object (`dtf`) and a date object as input and returns an array of formatted date parts. **Special JavaScript Feature: Template Literals** The regular expression replacement used in the third test case is part of the template literals feature, introduced in ECMAScript 2015 (ES6). This feature allows you to embed expressions inside string literals using backticks (`). **Pros and Cons of Each Approach** Here's a brief overview of each approach: 1. `dtf.formatToParts(jsDate)`: Pros: simple and efficient way to get the formatted date parts; Cons: may not be suitable for all use cases, as it returns an array of objects. 2. `dtf.format(jsDate)` followed by regular expression replacement: Pros: allows for more control over the formatting process; Cons: adds complexity and potential performance overhead due to the regular expression replacement. 3. `hackyOffset(dtf, jsDate)`: Pros: provides a modern and efficient way to work with dates using Luxon; Cons: requires additional library dependencies. **Other Alternatives** If you need more control over the formatting process or want to use a different library, consider these alternatives: * `moment.js`: A popular date formatting library that provides a wide range of formatting options. * `date-fns`: A modern date formatting library that offers a simple and efficient way to work with dates. Keep in mind that each alternative has its own strengths and weaknesses, so be sure to evaluate them based on your specific requirements.
Related benchmarks:
luxon-v-Intl format, luxon-v-Date native parse
luxon-v-Intl-v-datefns format, luxon-v-Date-v-datefns native parse
luxon vs datefns formatting
hello amila 2
Comments
Confirm delete:
Do you really want to delete benchmark?