Garry's Mod Wiki

Revision Difference

optimizationTips#564779

<cat>Dev.Lua</cat>⤶ <title>Concepts - Optimization Tips</title>⤶ ⤶ ⤶ # General Tips⤶ Just because something works doesn't mean it cant work _faster_⤶ ⤶ 1. **Use Local Varibles**: local variables are accessed by index instead of hash lookup.⤶ 2. **Avoid repeated heap allocations**: Creating tables or [closures](https://en.wikipedia.org/wiki/Closure_(computer_programming)) repeatedly can slow down performance.⤶ 3. **Prefer inline expressions over function calls**: For example: `tbl[#tbl + 1] = 0` is faster than <page text="table.insert(tbl, 0)" >table.insert</page>⤶ 4. **Multiplication Over Division**: `x * 0.5` is faster than `x / 2`⤶ 5. **Squaring over exponentiation**: `x * x` is faster than `x^2`⤶ 6. **Factor Expressions**: `x*y + x*z + y*z` can become `x*(y + z) + y*z`⤶ ⤶