From: Alanscut Date: Thu, 4 Jun 2020 03:45:43 +0000 (+0800) Subject: [feature] Optimize computeErasParse and computeMonthsParse X-Git-Tag: 2.30.0~17^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6c8b9c4543ff84558b8cc9a3954238307a5eafc;p=thirdparty%2Fmoment.git [feature] Optimize computeErasParse and computeMonthsParse --- diff --git a/src/lib/units/era.js b/src/lib/units/era.js index 1d2c9f2f6..bc05622a3 100644 --- a/src/lib/units/era.js +++ b/src/lib/units/era.js @@ -265,16 +265,22 @@ function computeErasParse() { mixedPieces = [], i, l, + erasName, + erasAbbr, + erasNarrow, eras = this.eras(); for (i = 0, l = eras.length; i < l; ++i) { - namePieces.push(regexEscape(eras[i].name)); - abbrPieces.push(regexEscape(eras[i].abbr)); - narrowPieces.push(regexEscape(eras[i].narrow)); - - mixedPieces.push(regexEscape(eras[i].name)); - mixedPieces.push(regexEscape(eras[i].abbr)); - mixedPieces.push(regexEscape(eras[i].narrow)); + erasName = regexEscape(eras[i].name); + erasAbbr = regexEscape(eras[i].abbr); + erasNarrow = regexEscape(eras[i].narrow); + + namePieces.push(erasName); + abbrPieces.push(erasAbbr); + narrowPieces.push(erasNarrow); + mixedPieces.push(erasName); + mixedPieces.push(erasAbbr); + mixedPieces.push(erasNarrow); } this._erasRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i'); diff --git a/src/lib/units/month.js b/src/lib/units/month.js index 68e275919..ff0f79ae8 100644 --- a/src/lib/units/month.js +++ b/src/lib/units/month.js @@ -304,27 +304,24 @@ function computeMonthsParse() { longPieces = [], mixedPieces = [], i, - mom; + mom, + shortP, + longP; for (i = 0; i < 12; i++) { // make the regex if we don't have it already mom = createUTC([2000, i]); - shortPieces.push(this.monthsShort(mom, '')); - longPieces.push(this.months(mom, '')); - mixedPieces.push(this.months(mom, '')); - mixedPieces.push(this.monthsShort(mom, '')); + shortP = regexEscape(this.monthsShort(mom, '')); + longP = regexEscape(this.months(mom, '')); + shortPieces.push(shortP); + longPieces.push(longP); + mixedPieces.push(longP); + mixedPieces.push(shortP); } // Sorting makes sure if one month (or abbr) is a prefix of another it // will match the longer piece. shortPieces.sort(cmpLenRev); longPieces.sort(cmpLenRev); mixedPieces.sort(cmpLenRev); - for (i = 0; i < 12; i++) { - shortPieces[i] = regexEscape(shortPieces[i]); - longPieces[i] = regexEscape(longPieces[i]); - } - for (i = 0; i < 24; i++) { - mixedPieces[i] = regexEscape(mixedPieces[i]); - } this._monthsRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i'); this._monthsShortRegex = this._monthsRegex;