My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
ByteScrum Technologies

106 likes

·

277 reads

5 comments

Philip Birk-Jensen
Philip Birk-Jensen
May 3, 2024

Some good generalized tips, however the example in "Optimize Loops" section seems to be off.

The two loops seems identical, except for the extra square variable. Maybe the wrong snippets have been pasted into the example, or maybe I'm missing something obvious?

12
·
·4 replies
ByteScrum Technologies
ByteScrum Technologies
Author
·May 4, 2024

Hi,

The first snippet calculates the square of i directly inside the loop's summing expression. This means that the multiplication operation i * i is performed each iteration.

The second snippet calculates the square of i and stores it in a separate variable square before adding it to the sum. This means that the multiplication operation is only performed once per iteration.

In terms of performance, the difference between the two approaches is likely negligible in this case. However, the second approach might be slightly more readable and maintainable, especially if the calculation of the square of i becomes more complex or is used in multiple places within the loop.

Thank you

·
Philip Birk-Jensen
Philip Birk-Jensen
May 6, 2024

I agree with the readability, especially if things start evolving into something more complex. I will always favor readability over micro optimization (at least in the initial part of development).

That being said, I still think the example is wrong, there's no calculations moved outside the loop, it's moved to a temporary variable. In both examples one multiplication and one addition is performed every loop iteration.

·
Emilla Filipowicz
Emilla Filipowicz
May 15, 2024

Hi, l've got some exciting news for you, l can teach you how you can turn your $150 into a whopping $4,600 in just 2 hours! Without interrupting your daily activities or sending money to anyone TEXT ME IF YOU ARE INTERESTED FOR MORE INFORMATION: 👇 WhatsApp No:+1 (209)-207-5967‬ Text No:+1 (209)-207-5967

WhatsApp link below 👇 👇👇👇 wa.me/message/PHQY33GUJPOGG1

·
Emilla Filipowicz
Emilla Filipowicz
May 15, 2024

Hi, l've got some exciting news for you, l can teach you how you can turn your $150 into a whopping $4,600 in just 2 hours! Without interrupting your daily activities or sending money to anyone TEXT ME IF YOU ARE INTERESTED FOR MORE INFORMATION: 👇 WhatsApp No:+1 (209)-207-5967‬ Text No:+1 (209)-207-5967

WhatsApp link below 👇 👇👇👇 wa.me/message/PHQY33GUJPOGG1

·