Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date creation speed
(version: 0)
Comparing performance of:
Plain JS vs Day Class
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
class DayModel { constructor(year, month, date) { this.year = year this.month = month this.date = date } /** * Checks if the passed CalendarDate is equal to itself. */ isEqual(day) { if (day) { return ( this.year === day.year && this.month === day.month && this.date === day.date ) } return false } toDate() { return new Date(this.year, this.month, this.date) } /** * Returns a new DayModel which is incremented based on the value passed. */ incrementBy(value) { // Creating new Javascript Date object to increment because // it will automatically take care of switching to next or previous // months & years without we having to worry about it. const date = new Date(this.year, this.month, this.date + value) return new DayModel(date.getFullYear(), date.getMonth(), date.getDate()) } /** * Clones the current day model. */ clone() { return new DayModel(this.year, this.month, this.date) } toComparisonString() { return `${this.year}${this.pad(this.month)}${this.pad(this.date)}` } toDateString() { return this.toDate().toLocaleDateString(undefined, { weekday: "long", month: "long", day: "numeric", year: "numeric" }) } /** * Compares the dates and returns boolean value based on the value passed */ isBefore(day, dayInclusive = false) { return dayInclusive ? this.toDate().getTime() <= day?.toDate().getTime() : this.toDate().getTime() < day?.toDate().getTime() } /** * Compares the dates and returns boolean value based on the value passed */ isAfter(day, dayInclusive = false) { return dayInclusive ? this.toDate().getTime() >= day?.toDate().getTime() : this.toDate().getTime() > day?.toDate().getTime() } pad(num) { return num < 10 ? `0${num}` : `${num}`; } } function getDaysInMonthJS(month, year) { const date = new Date(year, month, 1); const days = []; while (date.getMonth() === month) { days.push(new Date(date)); date.setDate(date.getDate() + 1); } return days; } function getDaysInMonthTS(month, year) { const nbD = new Date(year, month + 1, 0).getDate(); return Array(nbD) .fill(null) .map((_date, index) => { return new DayModel(year, month, index + 1); }); }
Tests:
Plain JS
getDaysInMonthJS(2024, 1)
Day Class
getDaysInMonthJS(2024, 1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Plain JS
Day Class
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Plain JS
7507373.5 Ops/sec
Day Class
6122255.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Definition** The benchmark definition is represented by the JSON provided at the top. It describes a class `DayModel` with various methods, including: * `constructor(year, month, date)` * `isEqual(day)` * `toDate()` * `incrementBy(value)` * `clone()` * `toComparisonString()` * `toDateString()` * `isBefore(day, dayInclusive = false)` * `isAfter(day, dayInclusive = false)` * `pad(num)` **Options being compared** The benchmark compares two options: 1. **Plain JS**: This option uses the built-in JavaScript functions to implement the `getDaysInMonth` function. It creates a new `Date` object for each month and increments it by 1 day in a while loop. 2. **Day Class**: This option uses a custom class `DayModel` that represents a date. The `getDaysInMonthJS` function creates an array of `DayModel` objects representing the days in the specified month. **Pros and Cons** Here are some pros and cons of each approach: * **Plain JS**: + Pros: Lightweight, easy to implement. + Cons: May be slower due to the overhead of creating new `Date` objects and the while loop. * **Day Class**: + Pros: More efficient, as it avoids the overhead of creating new `Date` objects and uses a more optimized data structure (an array of dates). + Cons: Requires more code and setup. **Library usage** The `getDaysInMonthJS` function uses a custom class `DayModel`, which is not a built-in JavaScript library. The purpose of this class is to provide a convenient way to represent dates and perform date-related operations. **Special JS feature or syntax** This benchmark does not use any special JavaScript features or syntax, such as async/await, Promises, or ES6 modules. **Alternatives** Other alternatives for implementing the `getDaysInMonth` function could be: * Using a library like moment.js * Using a built-in function like `Date.getMonth()` and `Date.getDate()` * Using a different data structure, such as an array of dates with a timestamp However, these alternatives may not provide the same level of efficiency or convenience as the `Day Class` approach.
Related benchmarks:
Object iteration methods
Object key access vs array index access
1 Math.min() + 1 Math.max() vs 1 Array.reduce()
Math.max() vs Array.reduce(Math.max)
new Intl.DateTimeFormat vs cached Intl.DateTimeFormat vs custom
Comments
Confirm delete:
Do you really want to delete benchmark?