Hello. In my game, there are multiple “object-specific” functions run from a main module. For example, when an object is created, this type of object may have a “Start” function, that runs code inside when created. But it may not. It may also have an “Update” function, ect.
My question is, is it more efficient to run blank functions for every object, or is it more efficient to check if the function exists before running it, and only run non-blank functions?
Efficient in terms of what? Most likely the difference is so small that it will not effect you use case. In most systems calling a function at all is already relatively expensive, so you wont notice much difference if you check for its existence first.
Efficiency in terms of memory. I just don’t know if running a function is more expensive or if running an if statement is more expensive. For me, it’s looping through potentially hundreds of objects every 1/10 of a second, which means the little bits of efficiency can stack up.
Its basically impossible to know for sure whether an if or function will cost more, they are both branches and branches are more costly than other statements. Your time is probably better spent finding a way to not have to do all these checks in the first place, or do them in parallel if you absolutely have to.
Its only effecient to check after a function, if you intend to use it. Im on mobile so this sample must do;
You have a modulescript with functions. And we got a serverscript. Its a obby stage game. We loop through all stages 1-10. With our serverscript. We check if the modulescript contains a function, with the name of out stage. If it does exist, we then send values over, or do something for that specific stage. If it does not exist we just skip it and proceed.
That can be used to create stage specific functions. But initializing it as you play. (Reach next stage ect.)
The reason is, that the serverscript will error otherwise, if the the function did not exist
So you should only check if a function exists, if you’re afraid of it not being present.
If your main goal is to keep memory usage low, I would avoid using blank functions because functions are unique in that no two functions are ever equal to each other and they each all have their own memory addresses. And seeing as you would probably need to maintain a reference to them in memory, they wouldn’t ever be garbage collected, so each blank function would always take up some memory even if a negligible amount.
Calling a function, even a blank one, will also typically take more time than it would take for an if-statement to check whether or not a value is truthy, so there’s that too.
local obj = {}
obj.func = function() end
local init = os.clock()
for i = 1, 100_000_000 do
obj.func()
end
local _end = os.clock()
print('100m blank function calls took', _end - init)
task.wait(1)
local obj = {}
local init = os.clock()
for i = 1, 100_000_000 do
if obj.func then
obj.func()
end
end
local _end = os.clock()
print('100m comparisons took', _end - init)