Skip to content

Commit

Permalink
Zone._index optimization: binary search for closest "until"
Browse files Browse the repository at this point in the history
  • Loading branch information
dashie committed Apr 20, 2019
1 parent c377ac8 commit fa5c4b6
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@
}
}

function closest(num, arr) {
if (num < arr[0]) {
return 0;
} else if (num >= arr[arr.length-1]) {
return -1;
}

var mid;
var lo = 0;
var hi = arr.length - 1;
while (hi - lo > 1) {
mid = Math.floor((lo + hi) / 2);
if (arr[mid] <= num) {
lo = mid;
} else {
hi = mid;
}
}
return hi;
}

Zone.prototype = {
_set : function (unpacked) {
this.name = unpacked.name;
Expand All @@ -158,10 +179,9 @@
untils = this.untils,
i;

for (i = 0; i < untils.length; i++) {
if (target < untils[i]) {
return i;
}
i = closest(target,untils);
if (i >= 0) {
return i;
}
},

Expand Down

0 comments on commit fa5c4b6

Please sign in to comment.