Skip to content

Commit

Permalink
Zone._index optimization: binary search for closest "until" (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashie committed May 21, 2023
1 parent 0b3bf1c commit ccaf698
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions moment-timezone.js
Expand Up @@ -150,6 +150,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 @@ -164,10 +185,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 ccaf698

Please sign in to comment.