From a847b81ec5076696763778df9a3aaf8060990ab9 Mon Sep 17 00:00:00 2001 From: Jamie Wong Date: Sun, 17 Nov 2013 18:43:43 -0500 Subject: [PATCH] Switch to moment 0.0.3 (available through github, not through website) This comes with a huge perf boost due to memoization! See: https://github.com/moment/moment-timezone/pull/39 --- server/static/js/ext/moment-timezone-data.js | 3 ++ server/static/js/ext/moment-timezone.js | 42 ++++++++++++++++---- server/static/js/rmc_moment.js | 1 - 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/server/static/js/ext/moment-timezone-data.js b/server/static/js/ext/moment-timezone-data.js index 84aca1c2..f0dc4743 100644 --- a/server/static/js/ext/moment-timezone-data.js +++ b/server/static/js/ext/moment-timezone-data.js @@ -1,4 +1,7 @@ define(["moment-timezone"], function (moment) { + // This was generated by http://momentjs.com/timezone/data/ + // It was also manually modified to get rid of all the rules we don't care + // about for performance resons. This means ignoring dates before 2007. moment.tz.add({ "zones": { "America/Toronto": [ diff --git a/server/static/js/ext/moment-timezone.js b/server/static/js/ext/moment-timezone.js index 9f1d385d..adaf5e4e 100644 --- a/server/static/js/ext/moment-timezone.js +++ b/server/static/js/ext/moment-timezone.js @@ -1,12 +1,12 @@ // moment-timezone.js -// version : 0.0.1 +// version : 0.0.3 // author : Tim Wood // license : MIT // github.com/timrwood/moment-timezone (function () { - var VERSION = "0.0.1"; + var VERSION = "0.0.3"; function onload(moment) { var oldZoneName = moment.fn.zoneName, @@ -53,6 +53,9 @@ this.timeRule = +timeRule; this.offset = parseMinutes(offset); this.letters = letters || ''; + this.date = memoize(this.date); + this.weekdayAfter = memoize(this.weekdayAfter); + this.lastWeekday = memoize(this.lastWeekday); } Rule.prototype = { @@ -135,6 +138,7 @@ function RuleSet (name) { this.name = name; this.rules = []; + this.lastYearRule = memoize(this.lastYearRule); } RuleSet.prototype = { @@ -252,6 +256,7 @@ this.offset = parseMinutes(offset); this.ruleSet = ruleSet; this.letters = letters; + this.lastRule = memoize(this.lastRule); for (i = 0; i < untilArray.length; i++) { untilArray[i] = +untilArray[i]; @@ -265,10 +270,7 @@ }, lastRule : function () { - if (!this._lastRule) { - this._lastRule = this.rule(this.until); - } - return this._lastRule; + return this.rule(this.until); }, format : function (rule) { @@ -288,6 +290,9 @@ this.name = normalizeName(name); this.displayName = name; this.zones = []; + this.zoneAndRule = memoize(this.zoneAndRule, function (mom) { + return +mom; + }); } ZoneSet.prototype = { @@ -328,6 +333,16 @@ Global Methods ************************************/ + function memoize (fn, keyFn) { + var cache = {}; + return function (first) { + var key = keyFn ? keyFn.apply(this, arguments) : first; + return key in cache ? + cache[key] : + (cache[key] = fn.apply(this, arguments)); + }; + } + function addRules (rules) { var i, j, rule; for (i in rules) { @@ -443,6 +458,15 @@ } }; + function getZoneSets() { + var sets = [], + zoneName; + for (zoneName in zoneSets) { + sets.push(zoneSets[zoneName]); + } + return sets; + } + moment.fn.tz = function (name) { if (name) { this._z = getZoneSet(name); @@ -475,12 +499,16 @@ for (i = 0; i < len; i++) { args[i] = arguments[i]; } - return moment.apply(null, args).tz(arguments[len]); + var m = moment.apply(null, args); + var preTzOffset = m.zone(); + m.tz(arguments[len]); + return m.add('minutes', m.zone() - preTzOffset); }; moment.tz.add = add; moment.tz.addRule = addRule; moment.tz.addZone = addZone; + moment.tz.zones = getZoneSets; moment.tz.version = VERSION; diff --git a/server/static/js/rmc_moment.js b/server/static/js/rmc_moment.js index f84ffea2..223c0e31 100644 --- a/server/static/js/rmc_moment.js +++ b/server/static/js/rmc_moment.js @@ -1,7 +1,6 @@ define(["moment", "moment-timezone", "ext/moment-timezone-data"], function (_moment, __, __) { // Always use America/Toronto as the timezone. return function rmc_moment(a, b, c, d) { - countMe("rmc_moment"); return _moment(a, b, c, d) .tz("America/Toronto"); };