Skip to content

speedometer-hud sends NUI messages far more often than its rate limit intends #64

Closed

Description

@TMHSDigital

Summary

The speedometer-hud example is presented as the model for "rate-limited SendNUIMessage", but its condition uses or, so it sends on every frame while the speed changes and also every 100 ms when the speed is unchanged.

Where

  • examples/speedometer-hud/client/main.lua:43-51
  • examples/speedometer-hud/fxmanifest.lua:6 (description: "rate-limited SendNUIMessage")

Details

-- Rate-limit: only push a 'speed' message when the displayed integer
-- changes, or at most once every 100ms to keep the readout fresh.
if speed ~= lastSpeed or (now - lastSent) >= 100 then
    lastSpeed = speed
    lastSent = now
    SendNUIMessage({ action = 'speed', value = speed, unit = Config.Unit })
end
Wait(0)
  • While accelerating or braking, the integer speed changes almost every frame, so the speed ~= lastSpeed branch sends 60+ messages per second with no limit.
  • At a steady speed, the or branch still sends about 10 identical messages per second, which do nothing in the UI (it only sets textContent).
  • The loop runs at Wait(0) with three natives per frame, although a speed readout does not need per-frame updates.

Impact

The cost per message is small, so runtime impact is low, but the example teaches the opposite of what it claims, and users copying it into heavier HUDs inherit the extra NUI traffic.

Steps to reproduce

  1. Add a counter around line 48 and print it once per second.
  2. Accelerate in a vehicle: the count tracks the frame rate. Hold a constant speed: about 10 identical sends per second.

Suggested fix

  • Send only when speed ~= lastSpeed and at least N ms have passed since the last send (and, not or); drop the unconditional 10 Hz refresh.
  • Use Wait(50) or Wait(100) while in a vehicle instead of Wait(0).
  • Make the comment and README match the code.

Acceptance criteria

  • At most one speed message per configured interval while driving, and none while the displayed value is unchanged.
  • Comment, README and manifest description describe the actual behaviour.

Getting started

  • Relevant files: examples/speedometer-hud/client/main.lua, examples/speedometer-hud/README.md
  • How to run checks: there is no test suite yet; run the MCP server with cd mcp-server && pip install -r requirements.txt && python server.py, and CI checks live in .github/workflows/validate.yml
  • Related docs: skills/nui-development/SKILL.md, skills/performance-optimization/SKILL.md

Filed by repo-audit/full-audit at 701824d. Severity: low. Effort: S. audit-key: performance|examples/speedometer-hud/client/main.lua|SendNUIMessage

Activity

  1. TMHSDigital commented on Nov 21, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in 8286f75.

    The HUD now polls every 100ms while driving and sends a speed message only when the displayed value changed and the interval passed (at most about 10 per second, none while steady); comments and README describe that.

    Verified: Reviewed the loop logic against the acceptance criteria (no FXServer available here to measure).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueperformanceseverity: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions