luxon
luxon copied to clipboard
weekdayLong is slow to execute
I'm finding that weekdayLong
is approximately two orders of magnitude slower that weekday
. This is especially apparent when processing large volumes of csv data.
Luxon version is 1.25.0
From a quick look at the code, I would guess it's because a Locale object is being created for each call.
const {DateTime } = require("luxon")
const NUM_TO_DAY = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday",
};
const date = DateTime.local();
console.time("weekdayLong");
const weekdayLong = date.weekdayLong;
console.log(weekdayLong);
console.timeEnd("weekdayLong");
console.time("weekdayLookup");
const weekdayLookup = NUM_TO_DAY[date.weekday];
console.log(weekdayLookup);
console.timeEnd("weekdayLookup");
Wednesday
weekdayLong: 5.281ms
Wednesday
weekdayLookup: 0.039ms
It's probably because it's using the Intl API, which is hella slow. For most English operations, we shortcutted that with hardcoded implementations. I wonder if that's not happening in this case...
weekdayLong is looking up from hardcoded data, but the Locale create does make it slower. Using the existing loc object from the DateTime
is roughly twice as quick over 100000 iterations.