]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[misc] optimize for loops (#5744)
authorPulkit Aggarwal <pulkitagg1995@gmail.com>
Sat, 24 Oct 2020 02:26:50 +0000 (07:56 +0530)
committerGitHub <noreply@github.com>
Sat, 24 Oct 2020 02:26:50 +0000 (19:26 -0700)
src/lib/create/from-string-and-array.js
src/lib/create/from-string-and-format.js
src/lib/create/from-string.js
src/lib/duration/valid.js
src/lib/moment/constructor.js
src/lib/moment/get-set.js
src/lib/parse/token.js
src/lib/utils/deprecate.js
src/lib/utils/is-moment-input.js
src/lib/utils/map.js

index c49caf3e9b477a1eecf72fd534250285422f1130..4e641674271b199d7e2d57fbc7ae88a44465fe5f 100644 (file)
@@ -12,15 +12,16 @@ export function configFromStringAndArray(config) {
         i,
         currentScore,
         validFormatFound,
-        bestFormatIsValid = false;
+        bestFormatIsValid = false,
+        configfLen = config._f.length;
 
-    if (config._f.length === 0) {
+    if (configfLen === 0) {
         getParsingFlags(config).invalidFormat = true;
         config._d = new Date(NaN);
         return;
     }
 
-    for (i = 0; i < config._f.length; i++) {
+    for (i = 0; i < configfLen; i++) {
         currentScore = 0;
         validFormatFound = false;
         tempConfig = copyConfig({}, config);
index 36d9be11e65f366ca38f435980a042563b22513b..82d60d52039344b7c246093116997023bb6d63a4 100644 (file)
@@ -41,12 +41,13 @@ export function configFromStringAndFormat(config) {
         skipped,
         stringLength = string.length,
         totalParsedInputLength = 0,
-        era;
+        era,
+        tokenLen;
 
     tokens =
         expandFormat(config._f, config._locale).match(formattingTokens) || [];
-
-    for (i = 0; i < tokens.length; i++) {
+    tokenLen = tokens.length;
+    for (i = 0; i < tokenLen; i++) {
         token = tokens[i];
         parsedInput = (string.match(getParseRegexForToken(token, config)) ||
             [])[0];
index 689bc9abd1e2f0fd73a37f6bbe97de10395b3a8c..881d833ab5eeb82fc65438a5d9c757ac42a8e038 100644 (file)
@@ -63,12 +63,13 @@ export function configFromISO(config) {
         allowTime,
         dateFormat,
         timeFormat,
-        tzFormat;
+        tzFormat,
+        isoDatesLen = isoDates.length,
+        isoTimesLen = isoTimes.length;
 
     if (match) {
         getParsingFlags(config).iso = true;
-
-        for (i = 0, l = isoDates.length; i < l; i++) {
+        for (i = 0, l = isoDatesLen; i < l; i++) {
             if (isoDates[i][1].exec(match[1])) {
                 dateFormat = isoDates[i][0];
                 allowTime = isoDates[i][2] !== false;
@@ -80,7 +81,7 @@ export function configFromISO(config) {
             return;
         }
         if (match[3]) {
-            for (i = 0, l = isoTimes.length; i < l; i++) {
+            for (i = 0, l = isoTimesLen; i < l; i++) {
                 if (isoTimes[i][1].exec(match[3])) {
                     // match[2] should be 'T' or space
                     timeFormat = (match[2] || ' ') + isoTimes[i][0];
index 13ee6cdef970ef2df947ba8d5f5e30546127f72f..88ef7a99a69c6ce1e82ea3fdbc3b88048dbd9738 100644 (file)
@@ -18,7 +18,8 @@ var ordering = [
 export default function isDurationValid(m) {
     var key,
         unitHasDecimal = false,
-        i;
+        i,
+        orderLen = ordering.length;
     for (key in m) {
         if (
             hasOwnProp(m, key) &&
@@ -31,7 +32,7 @@ export default function isDurationValid(m) {
         }
     }
 
-    for (i = 0; i < ordering.length; ++i) {
+    for (i = 0; i < orderLen; ++i) {
         if (m[ordering[i]]) {
             if (unitHasDecimal) {
                 return false; // only allow non-integers for smallest unit
index 7fd2eb55e32ac1ca2638c5f1ab9016f5d196cae2..b86de8b92b06be58bb307f14264e6d154a5c8632 100644 (file)
@@ -8,7 +8,10 @@ var momentProperties = (hooks.momentProperties = []),
     updateInProgress = false;
 
 export function copyConfig(to, from) {
-    var i, prop, val;
+    var i,
+        prop,
+        val,
+        momentPropertiesLen = momentProperties.length;
 
     if (!isUndefined(from._isAMomentObject)) {
         to._isAMomentObject = from._isAMomentObject;
@@ -41,8 +44,8 @@ export function copyConfig(to, from) {
         to._locale = from._locale;
     }
 
-    if (momentProperties.length > 0) {
-        for (i = 0; i < momentProperties.length; i++) {
+    if (momentPropertiesLen > 0) {
+        for (i = 0; i < momentPropertiesLen; i++) {
             prop = momentProperties[i];
             val = from[prop];
             if (!isUndefined(val)) {
index 0bdb65b9e83d3ebc7951093407b2f98240ed9df8..cbdd45520e12cd2c06080e986951bada4a5c9ea3 100644 (file)
@@ -58,8 +58,9 @@ export function stringSet(units, value) {
     if (typeof units === 'object') {
         units = normalizeObjectUnits(units);
         var prioritized = getPrioritizedUnits(units),
-            i;
-        for (i = 0; i < prioritized.length; i++) {
+            i,
+            prioritizedLen = prioritized.length;
+        for (i = 0; i < prioritizedLen; i++) {
             this[prioritized[i].unit](units[prioritized[i].unit]);
         }
     } else {
index b17066d6fbe3ca2d0e7819d23c3f6ca8e4724a36..ede35782e906308fb3215ece310494e7cff40d74 100644 (file)
@@ -6,7 +6,8 @@ var tokens = {};
 
 export function addParseToken(token, callback) {
     var i,
-        func = callback;
+        func = callback,
+        tokenLen;
     if (typeof token === 'string') {
         token = [token];
     }
@@ -15,7 +16,8 @@ export function addParseToken(token, callback) {
             array[callback] = toInt(input);
         };
     }
-    for (i = 0; i < token.length; i++) {
+    tokenLen = token.length;
+    for (i = 0; i < tokenLen; i++) {
         tokens[token[i]] = func;
     }
 }
index b2a8cfc63eafba8c695dab9cdc2eecb978ae0e9f..6f28236a940b62b1335b0483d22c71f718478ac3 100644 (file)
@@ -23,8 +23,9 @@ export function deprecate(msg, fn) {
             var args = [],
                 arg,
                 i,
-                key;
-            for (i = 0; i < arguments.length; i++) {
+                key,
+                argLen = arguments.length;
+            for (i = 0; i < argLen; i++) {
                 arg = '';
                 if (typeof arguments[i] === 'object') {
                     arg += '\n[' + i + '] ';
index c32b3d4afb398c3c83cc2be9043ba759880891dc..7543bdeab67e92af7b329878012f6280dea71c1e 100644 (file)
@@ -51,9 +51,10 @@ export function isMomentInputObject(input) {
             'ms',
         ],
         i,
-        property;
+        property,
+        propertyLen = properties.length;
 
-    for (i = 0; i < properties.length; i += 1) {
+    for (i = 0; i < propertyLen; i += 1) {
         property = properties[i];
         propertyTest = propertyTest || hasOwnProp(input, property);
     }
index 027458cf6aa1c8bbdf04d29ed2710292ec036890..14245fe2d6c1fd8346e1a29a531d2d1cf2220984 100644 (file)
@@ -1,7 +1,8 @@
 export default function map(arr, fn) {
     var res = [],
-        i;
-    for (i = 0; i < arr.length; ++i) {
+        i,
+        arrLen = arr.length;
+    for (i = 0; i < arrLen; ++i) {
         res.push(fn(arr[i], i));
     }
     return res;