Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Daily/Weekly using Date vs Math
(version: 7)
Comparing performance of:
Math vs Date Manipulation vs More Math
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var start_timestamp = new Date('2018-01-04T16:30:00.000Z'); var end_timestamp = new Date('2018-01-05T14:00:00.000Z'); var now1 = new Date('2018-06-05T11:30:00.000Z'); var now2 = new Date('2018-06-05T15:30:00.000Z'); var now3 = new Date('2018-06-04T11:30:00.000Z'); var now4 = new Date('2018-06-04T15:30:00.000Z'); var DAY = 24 * 60 * 60 * 1000; var WEEK = DAY * 7; var checkRepeated = (now, start, end, isDaily) => { const repeat = isDaily ? DAY : WEEK; if (now > start && now < end) { return true; } else if (now > start) { const count = Math.floor((now - start) / repeat); const newStart = (count * repeat) + start; const newEnd = (count * repeat) + end; if (now > newStart && now < newEnd) { return true; } } return false; }; var isBetween = (now, start, end, isDaily) => { if (isDaily) { if (start - end >= 86400000) { return true; } } else { const nowDay = now.getDay(); if (nowDay < start.getDay() || nowDay > end.getDay()) { return false; } } const time = now - (new Date(now)).setHours(0, 0, 0, 0); const startDate = (new Date(start)).setHours(0, 0, 0, 0); const startTime = start - startDate; const endDate = (new Date(end)).setHours(0, 0, 0, 0); const endTime = end - endDate; return time > startTime || (endDate > startDate && time < endTime); } var isBetweenMath = (now, start, end, isDaily) => { if (isDaily) { if (end - start >= 86400000) { return true; } } else { const nowDay = now.getDay(); if (nowDay < start.getDay() || nowDay > end.getDay()) { return false; } } const time = now % 86400000; const startTime = start % 86400000; const endTime = end % 86400000; return time > startTime || ((end - endTime) > (start - startTime) && time < endTime); }
Tests:
Math
checkRepeated(now1, start_timestamp, end_timestamp, true); checkRepeated(now2, start_timestamp, end_timestamp, true); checkRepeated(now3, start_timestamp, end_timestamp, true); checkRepeated(now4, start_timestamp, end_timestamp, true); checkRepeated(now1, start_timestamp, end_timestamp, false); checkRepeated(now2, start_timestamp, end_timestamp, false); checkRepeated(now3, start_timestamp, end_timestamp, false); checkRepeated(now4, start_timestamp, end_timestamp, false);
Date Manipulation
isBetween(now1, start_timestamp, end_timestamp, true); isBetween(now2, start_timestamp, end_timestamp, true); isBetween(now3, start_timestamp, end_timestamp, true); isBetween(now4, start_timestamp, end_timestamp, true); isBetween(now1, start_timestamp, end_timestamp, false); isBetween(now2, start_timestamp, end_timestamp, false); isBetween(now3, start_timestamp, end_timestamp, false); isBetween(now4, start_timestamp, end_timestamp, false);
More Math
isBetweenMath(now1, start_timestamp, end_timestamp, true); isBetweenMath(now2, start_timestamp, end_timestamp, true); isBetweenMath(now3, start_timestamp, end_timestamp, true); isBetweenMath(now4, start_timestamp, end_timestamp, true); isBetweenMath(now1, start_timestamp, end_timestamp, false); isBetweenMath(now2, start_timestamp, end_timestamp, false); isBetweenMath(now3, start_timestamp, end_timestamp, false); isBetweenMath(now4, start_timestamp, end_timestamp, false);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math
Date Manipulation
More Math
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):
I'll provide an explanation of the benchmark and its various aspects. **Benchmark Overview** The provided benchmark measures the performance of three different approaches for checking whether a given date falls within a specific time range. The benchmark consists of two main parts: 1. **`checkRepeated` function**: This function takes four arguments: `now`, `start`, `end`, and an optional boolean `isDaily`. It checks if `now` is between `start` and `end` (inclusive) or within a repeated interval of the specified duration. 2. **`isBetween` function**: This function also takes four arguments: `now`, `start`, `end`, and an optional boolean `isDaily`. It checks if `now` falls within the time range defined by `start` and `end`. 3. **`isBetweenMath` function**: This function is similar to `isBetween`, but it uses a different approach that involves calculating the remainder of `now` when divided by the duration. **Comparison of Approaches** The benchmark compares the performance of these three approaches: * **`checkRepeated`**: This approach calculates the difference between `now` and `start` or `end`, depending on whether `isDaily` is true. It then divides this difference by the repeated interval and checks if the result is greater than 0. * **`isBetween`**: This approach uses a more complex calculation involving the day of the week, start and end dates, and time components. It first calculates the number of seconds between `now` and midnight (to normalize the date), then compares this value with the duration defined by `start` and `end`. * **`isBetweenMath`**: This approach uses a simple modulo operation to calculate the remainder of `now` when divided by the duration. It then checks if this remainder falls within a specific range. **Performance Results** The latest benchmark results show that: * The **`More Math`** approach performs the best, with an average of 173681.265625 executions per second. * The **`Date Manipulation`** approach has an average performance of 43178.01953125 executions per second. * The **`Math`** approach has the lowest performance, averaging 34977.10546875 executions per second. These results indicate that the `More Math` approach is significantly faster than the other two approaches, which suggests that its simplicity and optimized calculations contribute to its better performance. The benchmark can be seen as a representation of how developers might use similar functions in their codebases.
Related benchmarks:
Check date comparison
dayjs vs luxon vs js-joda vs date-fns (comparing)
dayjs vs luxon vs js-joda vs date-fns (calculating)
JS Date libraries 2 fixed
Comments
Confirm delete:
Do you really want to delete benchmark?