Modulescript performance query

Hi, I’m familiar with the general idea that you shouldn’t write the same code out multiple times, and should reuse stuff when possible.

From what I’ve seen, it seems like a good practice to move functions that will be used by many clones to a module script so it’s only written out once. But this has me wondering about performance.

Lets ignore how modulescripts would be the more ergonomic and potentially cleaner solution (for making changes etc.) for a moment.

In my mind, I see a modulescript being “called” constantly from a lot of clones, getting a lot of “traffic”, and slowing down performance. Is this a realistic mindset?

I know it will depend on the content and complexity of the function (eg. complex math) but I’m asking about this in general.

If a function is going to be used almost constantly, is this a reason to have the function in each script rather than having to “reach out” to the modulescript almost constantly? To use an analogy: instead of having to walk to the printing shop all the time, you just have a printer at home to do it yourself.

TLDR: Does it hurt performance to constantly access a modulescript as opposed to copying a constantly used function out everywhere? When shouldn’t modulescripts be used?


1 Like

Module scripts cache the result, meaning the script only runs once no matter how many requires from how many different scripts. So yeah it doesn’t matter if it’s called constantly because it only happens once.

Try it out, pretty interesting:

Module setup:

local module = {}

print("Accessed module!")

return module

--script accessing module, called "Other"

local module = require(script.Parent)--script.Parent = module1

local module2 = require(script.Parent)


--In another script acessing module 1, called "Script"
local module3 = require(script.Parent)

The result is this:

  13:40:54.021  Accessed module!  -  Server - ModuleScript:3
  13:40:54.022  true  -  Server - Other:6

The print only happens once :shock:

I am willing to be reassured by this information, but I’m not sure it can be applied to my situation though. I said “calling” to mean using the module, but I think that can be misunderstood as just to mean writing out the “___ = require()”

I’m more looking at the performance within repeat-until loops that are constantly feeding in changing variables for the modulescript function to process and return. eg “module.thefunction(info1,info2)”

Thanks for making this apparent though.

Oh, well I see your point. Localizing does indeed make it faster to access a variable but not by much. I believe the situation should be similar to localizing a global variable.

Only way to find out if the gain in performance is the same for a module script should be to benchmark it using os.clock() (the preferred method) using module.function() vs function().

I believe assuming the situation is the same that the gain in performance should be negligible relative to the organizational benefits a module script provides.

So to answer the question yes it does hurt performance though we can ignore it since it’s negligible.

I see. I think this is what I’m glad to/wanted to hear. Cheers :+1:
I’ll mark this as the solution but I hope others add their input. From what I’ve picked up, a good consensus if to module “global” things when possible- but I hope someone pops up with when they shouldn’t be used.