]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] Optimize computeErasParse and computeMonthsParse
authorAlanscut <wp_scut@163.com>
Thu, 4 Jun 2020 03:45:43 +0000 (11:45 +0800)
committerIskren Chernev <me@iskren.info>
Sat, 23 Dec 2023 13:18:05 +0000 (15:18 +0200)
src/lib/units/era.js
src/lib/units/month.js

index 1d2c9f2f671892321efc4658b010abe9c4766fd2..bc05622a37c3f1da80f92676aaa25a610cf6d310 100644 (file)
@@ -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');
index 68e275919a856ef00d76c0c7704b3f7a071ad8ab..ff0f79ae870ce2e844fc73ce99fa8a88d1a6e776 100644 (file)
@@ -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;