Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optimized getMarkedDates Function
(version: 0)
Comparing performance of:
Original Function vs New Version
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const daySlots = { calendar: { calendar_items: [{ weekday_id: 1 }, { weekday_id: 3 }, { weekday_id: 5 }, ], date_range_days: 30, }, };
Tests:
Original Function
const getMarkedDates = () => { let markedDatesObj = {}; let maxDate = null; if (daySlots && daySlots?.calendar?.calendar_items) { const daysOfWeek = [0, 1, 2, 3, 4, 5, 6]; const workingDays = daySlots?.calendar?.calendar_items.map( item => item.weekday_id, ); const disabledDays = daysOfWeek.filter( day => !workingDays.includes(day), ); const currentDate = new Date(); const lastDate = new Date(currentDate); lastDate.setDate( currentDate.getDate() + daySlots?.calendar?.date_range_days, ); maxDate = lastDate.toISOString().slice(0, 10); for ( let d = new Date(currentDate); d <= lastDate; d.setDate(d.getDate() + 1) ) { const currentDay = d.getDay(); if (disabledDays.includes(currentDay)) { const dateString = d.toISOString().slice(0, 10); markedDatesObj = { ...markedDatesObj, [dateString]: { disabled: true, disableTouchEvent: true, textColor: 'red', color: 'transparent', }, }; } } } return {markedDates: markedDatesObj, maxDate}; };
New Version
const getMarkedDates = () => { const markedDatesObj = {}; let maxDate = null; if (daySlots?.calendar?.calendar_items) { const daysOfWeek = [0, 1, 2, 3, 4, 5, 6]; const workingDays = daySlots.calendar.calendar_items.map(item => item.weekday_id); const disabledDays = daysOfWeek.filter(day => !workingDays.includes(day)); const currentDate = new Date(); const lastDate = new Date(currentDate); lastDate.setDate(currentDate.getDate() + daySlots.calendar.date_range_days); maxDate = lastDate.toISOString().slice(0, 10); const iterateDate = new Date(currentDate); while (iterateDate <= lastDate) { const currentDay = iterateDate.getDay(); if (disabledDays.includes(currentDay)) { const dateString = iterateDate.toISOString().slice(0, 10); markedDatesObj[dateString] = { disabled: true, disableTouchEvent: true, textColor: 'red', color: 'transparent', }; } iterateDate.setDate(iterateDate.getDate() + 1); } } return { markedDates: markedDatesObj, maxDate }; };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Original Function
New Version
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of two versions of a JavaScript function, `getMarkedDates`, which is used to generate marked dates for a calendar. The benchmark compares the execution speed and throughput of these two versions. **Options Compared** There are two main options being compared: 1. **Original Function**: This version uses a `for` loop to iterate through each day in the date range, checking if it's disabled before adding it to the `markedDatesObj`. 2. **New Version**: This version uses a `while` loop to iterate through each day in the date range, also checking if it's disabled before adding it to the `markedDatesObj`. **Pros and Cons of Each Approach** 1. **Original Function** * Pros: + Easier to understand and maintain due to its simple `for` loop structure. * Cons: + May be slower due to the overhead of the `for` loop iteration. 2. **New Version** * Pros: + Can potentially be faster due to the use of a more efficient `while` loop iteration. * Cons: + More complex and harder to understand due to its nested loop structure. **Library Usage** The benchmark uses no external libraries, so there is no library usage to discuss. **Special JavaScript Feature/Syntax** There is no special JavaScript feature or syntax being used in this benchmark. The code follows standard ECMAScript syntax and conventions. **Other Alternatives** If the `getMarkedDates` function were to be implemented using a different approach, some alternatives could include: * Using a more efficient data structure, such as a `Set`, to store the disabled dates. * Utilizing parallel processing or multi-threading to take advantage of multiple CPU cores. * Leveraging JavaScript's built-in optimization techniques, such as inlining and caching. However, these alternatives would require significant changes to the codebase and may not necessarily result in improved performance.
Related benchmarks:
JS Date libraries 2
Date libraries benchmark JS 1
JS Date libraries 2023.0
JS Date libraries 2 fixed
Comments
Confirm delete:
Do you really want to delete benchmark?