Skip to content

Commit 287820a

Browse files
committed
perf: Skip binary search when timestamp is after last transition
Many zones don't have any future transitions (i.e. all their transitions are in the past, and they don't have any planned DST changes). For these zones, looping over the `untils` array is pointless for any "now-ish" timestamp. If the timestamp being checked is after the last `until`, we can skip the binary search completely.
1 parent ccaf698 commit 287820a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

moment-timezone.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,19 @@
150150
}
151151
}
152152

153-
function closest(num, arr) {
153+
function closest (num, arr) {
154+
var len = arr.length;
154155
if (num < arr[0]) {
155156
return 0;
156-
} else if (num >= arr[arr.length-1]) {
157+
} else if (len > 1 && arr[len - 1] === Infinity && num >= arr[len - 2]) {
158+
return len - 1;
159+
} else if (num >= arr[len - 1]) {
157160
return -1;
158161
}
159162

160163
var mid;
161164
var lo = 0;
162-
var hi = arr.length - 1;
165+
var hi = len - 1;
163166
while (hi - lo > 1) {
164167
mid = Math.floor((lo + hi) / 2);
165168
if (arr[mid] <= num) {
@@ -185,7 +188,7 @@
185188
untils = this.untils,
186189
i;
187190

188-
i = closest(target,untils);
191+
i = closest(target, untils);
189192
if (i >= 0) {
190193
return i;
191194
}

0 commit comments

Comments
 (0)