From fa5c4b69b6e42760a3dcc8f818e921759e7e604d Mon Sep 17 00:00:00 2001 From: dashie Date: Mon, 28 Jan 2019 10:41:27 +0100 Subject: [PATCH] Zone._index optimization: binary search for closest "until" --- moment-timezone.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/moment-timezone.js b/moment-timezone.js index faf02ea9..0d2a5198 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -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; @@ -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; } },