Skip to Content
PerformanceCode Optimization

Code Optimization

Optimize your code for better server performance. Measure each change with resmon under representative player load before treating it as an optimization, and compare results against a repeatable baseline.

Avoid while(true) Loops

Bad

while true do Wait(0) -- Do something every frame end

Good

CreateThread(function() while true do Wait(1000) -- Wait 1 second -- Do something end end)

Throttling

Prevent excessive function calls:

local lastCall = 0 function throttledAction() local currentTime = GetGameTimer() if currentTime - lastCall < 1000 then return end lastCall = currentTime -- Do action end

Caching Natives

Cache expensive native calls:

-- Bad: Called every frame local coords = GetEntityCoords(PlayerPedId()) -- Good: Cache and update periodically local cachedCoords = vector3(0, 0, 0) CreateThread(function() while true do Wait(100) cachedCoords = GetEntityCoords(PlayerPedId()) end end)

Best Practices

  • Use appropriate Wait() times
  • Cache expensive operations
  • Throttle frequent calls
  • Profile your code