luxon icon indicating copy to clipboard operation
luxon copied to clipboard

weekdayLong is slow to execute

Open rooch84 opened this issue 4 years ago • 2 comments

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

rooch84 avatar Sep 16 '20 07:09 rooch84

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...

icambron avatar Sep 19 '20 14:09 icambron

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.

dan-overton avatar Feb 21 '21 22:02 dan-overton