Skip to content

Commit fa5c4b6

Browse files
committed
Zone._index optimization: binary search for closest "until"
1 parent c377ac8 commit fa5c4b6

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

moment-timezone.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,27 @@
144144
}
145145
}
146146

147+
function closest(num, arr) {
148+
if (num < arr[0]) {
149+
return 0;
150+
} else if (num >= arr[arr.length-1]) {
151+
return -1;
152+
}
153+
154+
var mid;
155+
var lo = 0;
156+
var hi = arr.length - 1;
157+
while (hi - lo > 1) {
158+
mid = Math.floor((lo + hi) / 2);
159+
if (arr[mid] <= num) {
160+
lo = mid;
161+
} else {
162+
hi = mid;
163+
}
164+
}
165+
return hi;
166+
}
167+
147168
Zone.prototype = {
148169
_set : function (unpacked) {
149170
this.name = unpacked.name;
@@ -158,10 +179,9 @@
158179
untils = this.untils,
159180
i;
160181

161-
for (i = 0; i < untils.length; i++) {
162-
if (target < untils[i]) {
163-
return i;
164-
}
182+
i = closest(target,untils);
183+
if (i >= 0) {
184+
return i;
165185
}
166186
},
167187

0 commit comments

Comments
 (0)