Luxon vs Naked

Benchmark created on


Description

Using Luxon to do date calculations > 52 weeks may be expensive. lets find out how expensive

Preparation HTML

<script src="https://cdn.jsdelivr.net/npm/luxon@3.4.4/build/global/luxon.min.js"></script>

<div id="thing"></div>

Setup

const MS_IN_SECOND = 1000;
const MS_IN_MINUTE = MS_IN_SECOND * 60;
const MS_IN_HOUR = MS_IN_MINUTE * 60;
const MS_IN_DAY = MS_IN_HOUR * 24;
const MS_IN_WEEK = MS_IN_DAY * 7;
const future = luxon.DateTime.local(2025, 1, 1);
const segments = ["years", "months", "weeks", "days", "hours", "minutes", "seconds"];

Test runner

Ready to run.

Testing in
TestOps/sec
Naked
const now = Date.now()
const remaining = Math.max(1735689600000 - now, 0);

var weeks = Math.floor(remaining / MS_IN_WEEK);
var days = Math.floor((remaining % MS_IN_WEEK) / MS_IN_DAY);
var hours = Math.floor((remaining % MS_IN_DAY) / MS_IN_HOUR);
var minutes = Math.floor((remaining % MS_IN_HOUR) / MS_IN_MINUTE);
var seconds = Math.ceil((remaining % MS_IN_MINUTE) / MS_IN_SECOND);
thing.innerText = `Weeks ${weeks} Days ${days} Hours ${hours} Minutes ${minutes} Seconds ${seconds}`;
ready
Luxon
const now = luxon.DateTime.now();
const diff = luxon.Interval.fromDateTimes(now, future);
const duration = diff.toDuration(segments);

const years = duration.get('years');
const months = duration.get('months');
const weeks = duration.get('weeks');
const days = duration.get('days');
const hours = duration.get('hours');
const minutes = duration.get('minutes');
const seconds = duration.get('seconds');

thing.innerText = `Years ${years} Months ${months} Weeks ${weeks} Days ${days}  Hours ${hours} Seconds ${Math.ceil(seconds)}`;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.