Myzwer / foothillschurch

Bootcamp II is a wordpress theme (as well as an inside joke) designed to suit the needs of foothillschurch.com. It makes use of webpack, Babel, Sass, Tailwind, Browsersync, PostCSS, ESLint, Stylelint, Prettier and more. It is meant for that site, but if you can use it by all means go for it.
1 stars 1 forks source link

Declare static variables outside of loops #68

Closed rain2o closed 2 weeks ago

rain2o commented 1 month ago

This is a very minor example that won't really make a noticeable difference. However, it's a good practice to get into because other situations could cause performance impacts.

https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/components/blocks/event-type.php#L80-L84

You're declaring the $size_select variable in each iteration of the loop, but the variable is exactly the same in each iteration. If this variable used something from in the loop, like the post for example, then it would need to be in the loop. But, this variable doesn't care about the loop data, it's just a static variable that's the same each time. Because of this, it is more optimal to declare the variable once, outside of the loop, and then you can use it as needed.

Myzwer commented 2 weeks ago

Makes sense. Good to know.