Skip to content

Commit

Permalink
perf: Avoid redundant offset/abbr checks in tz.guess (non-Intl fallback)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilmoreorless committed May 21, 2023
1 parent 287820a commit 8b39cdc
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,21 @@
function userOffsets() {
var startYear = new Date().getFullYear() - 2,
last = new OffsetAt(new Date(startYear, 0, 1)),
lastOffset = last.offset,
offsets = [last],
change, next, i;
change, next, nextOffset, i;

for (i = 1; i < 48; i++) {
next = new OffsetAt(new Date(startYear, i, 1));
if (next.offset !== last.offset) {
nextOffset = new Date(startYear, i, 1).getTimezoneOffset();
if (nextOffset !== lastOffset) {
// Create OffsetAt here to avoid unnecessary abbr parsing before checking offsets
next = new OffsetAt(new Date(startYear, i, 1));
change = findChange(last, next);
offsets.push(change);
offsets.push(new OffsetAt(new Date(change.at + 6e4)));
last = next;
lastOffset = nextOffset;
}
last = next;
}

for (i = 0; i < 4; i++) {
Expand Down Expand Up @@ -357,15 +361,21 @@
var offsetsLength = offsets.length,
filteredGuesses = {},
out = [],
i, j, guessesOffset;
checkedOffsets = {},
i, j, offset, guessesOffset;

for (i = 0; i < offsetsLength; i++) {
guessesOffset = guesses[offsets[i].offset] || {};
offset = offsets[i].offset;
if (checkedOffsets.hasOwnProperty(offset)) {
continue;
}
guessesOffset = guesses[offset] || {};
for (j in guessesOffset) {
if (guessesOffset.hasOwnProperty(j)) {
filteredGuesses[j] = true;
}
}
checkedOffsets[offset] = true;
}

for (i in filteredGuesses) {
Expand Down

0 comments on commit 8b39cdc

Please sign in to comment.