What's the overhead on object properties vs lua variables?

I’m curious on the difference between ROBLOX object properties vs Lua variables, and if using object properties should be avoided as much as reasonably possible or only adds a negligible amount of overhead.

Workspace.box.LineForce.Attachment0.Parent.Color

Is the userdata a direct map to the objects in ROBLOX’s memory, or a reference that has to be created when looked up?

Accessing instance properties is quite slow, compared to table access to a local Lua table. It is much faster to store the property as a variable if you’re accessing it multiple times (and it isn’t dynamic). You can’t really optimise instance methods any further, however.

This is more empirical advice than anything, so it would pay to test the performance for your individual use case. Not a Lua expert in this regard, but I have played around with micro-optimisations.

In practical terms, I would say it’s unlikely to be slowing down your code, but it really does depend on your use case–I can’t think of any time I’ve accessed properties intensively enough to cause a worry regarding performance.

I would definitely recommend poking around the micro-profiler to see what the slowest performing parts of your code are, before optimising prematurely.

There’s an RDC video on the new Lua VM if you’re interested, I believe they talk about the performance of instance properties at some point. Access to the reflection layer has apparently become much faster with the new Lua VM released by Roblox.

3 Likes