Skip to content

Conversation

@PulkitAgg
Copy link
Contributor

This pull request will improve the performance as I am pre calculating the array length before the for loop execution
For example

let i, arr = [1,2,3,4,5];
for(i = 0; i< arr.length; i++) {
    console.log(i)
}

arr.length will execute in each iteration which slows our execution. For this I just pre calculate the length and use that variable in each iteration like

let i, arr = [1,2,3,4,5], arrLen = arr.length;
for(i = 0; i< arrLen; i++) {
    console.log(i)
}

I am also attachig the test suite case run time which is decrease by 610ms after changes

Before
Test_Case_Run_Time_Before_Code_Update

After
Test_Case_Run_Time_After_Code_Update

@jsf-clabot
Copy link

jsf-clabot commented Oct 2, 2020

CLA assistant check
All committers have signed the CLA.

@PulkitAgg PulkitAgg changed the title Performance optimization misc/Performance optimization Oct 2, 2020
@PulkitAgg PulkitAgg changed the title misc/Performance optimization [misc] Performance optimization Oct 2, 2020
@coveralls
Copy link

coveralls commented Oct 2, 2020

Coverage Status

Coverage increased (+0.006%) to 88.499% when pulling 5d71a7b on PulkitAgg:develop into 116752c on moment:develop.

@marwahaha
Copy link
Member

Thanks for this. How did you find these cases? Did you look manually? Is there a lint rule we can set up to avoid this in the future?

@PulkitAgg
Copy link
Contributor Author

@marwahaha I look manually for these cases. I don't have any idea about such rule but we can look for this rule.

tzFormat;
tzFormat,
isoDateLen = isoDates.length,
isoTimeLen = isoTimes.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change these to isoDatesLen and isoTimesLen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marwahaha I have updated these variables

@marwahaha marwahaha changed the title [misc] Performance optimization [misc] optimize for loops Oct 24, 2020
@marwahaha marwahaha merged commit c1f4568 into moment:develop Oct 24, 2020
@lifeart
Copy link

lifeart commented Oct 26, 2020

may be related - https://mrale.ph/blog/2014/12/24/array-length-caching.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants