Caching means storing the result of an operation (like an array length or function return) in a variable outside the loop, so it only has to be calculated once.
Optimizing Loop Termination Conditions
What does caching a variable mean?
The answer
More FAQs in Optimizing Loop Termination Conditions
The loop condition is evaluated at the start of the loop and before every subsequent iteration. If it runs 100 times, the condition is evaluated 101 times.
Because the function will be executed on every single iteration. If the function is slow or computationally heavy, it ruins the algorithm's performance.
In modern languages (Java, Python, JS, C#), array length is an O(1) property lookup, so it is extremely fast and safe to use in the condition.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.