From: Pulkit Aggarwal Date: Sat, 24 Oct 2020 02:26:50 +0000 (+0530) Subject: [misc] optimize for loops (#5744) X-Git-Tag: 2.29.2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1f456813174431c41212f99d9b0056f90f8d14c;p=thirdparty%2Fmoment.git [misc] optimize for loops (#5744) --- diff --git a/src/lib/create/from-string-and-array.js b/src/lib/create/from-string-and-array.js index c49caf3e9..4e6416742 100644 --- a/src/lib/create/from-string-and-array.js +++ b/src/lib/create/from-string-and-array.js @@ -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); diff --git a/src/lib/create/from-string-and-format.js b/src/lib/create/from-string-and-format.js index 36d9be11e..82d60d520 100644 --- a/src/lib/create/from-string-and-format.js +++ b/src/lib/create/from-string-and-format.js @@ -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]; diff --git a/src/lib/create/from-string.js b/src/lib/create/from-string.js index 689bc9abd..881d833ab 100644 --- a/src/lib/create/from-string.js +++ b/src/lib/create/from-string.js @@ -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]; diff --git a/src/lib/duration/valid.js b/src/lib/duration/valid.js index 13ee6cdef..88ef7a99a 100644 --- a/src/lib/duration/valid.js +++ b/src/lib/duration/valid.js @@ -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 diff --git a/src/lib/moment/constructor.js b/src/lib/moment/constructor.js index 7fd2eb55e..b86de8b92 100644 --- a/src/lib/moment/constructor.js +++ b/src/lib/moment/constructor.js @@ -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)) { diff --git a/src/lib/moment/get-set.js b/src/lib/moment/get-set.js index 0bdb65b9e..cbdd45520 100644 --- a/src/lib/moment/get-set.js +++ b/src/lib/moment/get-set.js @@ -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 { diff --git a/src/lib/parse/token.js b/src/lib/parse/token.js index b17066d6f..ede35782e 100644 --- a/src/lib/parse/token.js +++ b/src/lib/parse/token.js @@ -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; } } diff --git a/src/lib/utils/deprecate.js b/src/lib/utils/deprecate.js index b2a8cfc63..6f28236a9 100644 --- a/src/lib/utils/deprecate.js +++ b/src/lib/utils/deprecate.js @@ -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 + '] '; diff --git a/src/lib/utils/is-moment-input.js b/src/lib/utils/is-moment-input.js index c32b3d4af..7543bdeab 100644 --- a/src/lib/utils/is-moment-input.js +++ b/src/lib/utils/is-moment-input.js @@ -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); } diff --git a/src/lib/utils/map.js b/src/lib/utils/map.js index 027458cf6..14245fe2d 100644 --- a/src/lib/utils/map.js +++ b/src/lib/utils/map.js @@ -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;