Skip to content

Commit

Permalink
[misc] optimize for loops (moment#5744)
Browse files Browse the repository at this point in the history
  • Loading branch information
PulkitAgg authored Oct 24, 2020
1 parent 5d811c8 commit c1f4568
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/lib/create/from-string-and-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/lib/create/from-string-and-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 5 additions & 4 deletions src/lib/create/from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down
5 changes: 3 additions & 2 deletions src/lib/duration/valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/lib/moment/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/moment/get-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/parse/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var tokens = {};

export function addParseToken(token, callback) {
var i,
func = callback;
func = callback,
tokenLen;
if (typeof token === 'string') {
token = [token];
}
Expand All @@ -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;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '] ';
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/is-moment-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/map.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit c1f4568

Please sign in to comment.