| 1 | --- |
| 2 | title: Cache Property Access in Loops |
| 3 | impact: LOW-MEDIUM |
| 4 | impactDescription: reduces lookups |
| 5 | tags: javascript, loops, optimization, caching |
| 6 | --- |
| 7 | |
| 8 | ## Cache Property Access in Loops |
| 9 | |
| 10 | Cache object property lookups in hot paths. |
| 11 | |
| 12 | **Incorrect (3 lookups × N iterations):** |
| 13 | |
| 14 | ```typescript |
| 15 | for (let i = 0; i < arr.length; i++) { |
| 16 | process(obj.config.settings.value) |
| 17 | } |
| 18 | ``` |
| 19 | |
| 20 | **Correct (1 lookup total):** |
| 21 | |
| 22 | ```typescript |
| 23 | const value = obj.config.settings.value |
| 24 | const len = arr.length |
| 25 | for (let i = 0; i < len; i++) { |
| 26 | process(value) |
| 27 | } |
| 28 | ``` |
| 29 |