Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize printFib using lists to build strings #14

Merged
merged 3 commits into from
Oct 6, 2021

Conversation

saranshsaini
Copy link
Contributor

Adding to a string each time creates an entirely new string because strings are immutable in JS. Instead, we can just add to a list and turn it into a string at the end.

num1 = num2;
num2 = num3;
}
return("Fibonacci Series : "+st);
return("Fibonacci Series : "+st.join(' '));

Choose a reason for hiding this comment

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

I think there is no optimization here. and through the string.join() you have added another loop if you are not satisfied with my opinion please let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When you add to a string like s=s+'a' , you have to create a whole new new string, which is not a problem with short strings but can get a little inefficient with long strings, so with big strings adding to a list is constant time rather than making a whole new string.
The optimization is not that big, and in modern systems it is not that big of a difference, so if you want you can ignore this pull or include it, up to you.

Choose a reason for hiding this comment

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

Yeah, you're right :)

@suryapratapsinghsuryavanshi suryapratapsinghsuryavanshi merged commit 9528442 into suryapratapsinghsuryavanshi:main Oct 6, 2021
@suryapratapsinghsuryavanshi suryapratapsinghsuryavanshi linked an issue Oct 6, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide more relative Method
2 participants