Why does this code run slower when inside a ModuleScript?

I was doing some performance tests, and I noticed that some code I had inside a ModuleScript took a longer time to finish than when the code was not in the ModuleScript. This confused me as I could think of no reason that this code would run significantly slower in a ModuleScript than in a normal script itself.

The code being ran is a simple loop that increments a variable each iteration. The code in the normal script and ModuleScript is shown below.

local module = {}

function module.test()
	local t = tick()
	local n = 0
	for i = 1, 1000000 do
		n = n + 1
	end
	print("time", tick() - t)
end

print("TEST1")
module.test()

print("TEST2")
require(script.Parent.ModuleScript).test()

MODULE

local module = {}

function module.test()
	local t = tick()
	local n = 0
	for i = 1, 1000000 do
		n = n + 1
	end
	print("time", tick() - t)
end

return module

The output was as follows:

TEST1
time 0.14418029785156
TEST2
time 0.48594498634338

I also tried changing the order in which they ran, but that did not have any signficant changes. What exactly is the cause for this, and how do ModuleScripts run?

EDIT: I would also like to specify that both the script and ModuleScript are in ServerScriptService, and this is being run on an empty place in Play Solo.

1 Like

What I can tell how ModuleScripts run: the require function(in normal Lua, I think it should be the same in Roblox’ Lua) loads a file(reads it)

About the perfermane: The require can take some time, but it shouldn’t be such a difference, I would suggest you make more over 100-1000 tests and take the average
A reason could be, that your computer does some background work or garbage collection is active, such things slow the performance down

1 Like

I created a completely empty place and put a Script and a Module Script in the ServerScriptService with your functions and took 10 tests:
Screenshot_10

Although the Script finished first 2 more times than the Module Script, the Module Script ended up having the lowest average time. Your times are a LOT higher than any of these so there are a definitely a few variables missing within this data(I also only took 10 tests, usually it should be a lot more). One variable is most likely the system being used to run Studio. I would do what @borgerding reccomended, check some background processes and take some more tests.

Edit: The bottom right is not the average Difference it is the Difference between the Average Times(Accidentally made the color a different shade of purple)

5 Likes

I published the place and joined it to see if it made a difference, and it did. The two times were about the same when I tested online versus in studio (I ran the code on the server first, and then the client). You guys are probably right, I never knew that background processes could affect the time it takes for modules to run in studio.

When doing this kind of work turn off the Lua debugger in the settings. It will provide you with a more stable comparison between tests.

Lua Debugger

Lua Debugger enabled

RobloxStudioBeta_aIZKEsWDs4

Lua Debugger disabled

RobloxStudioBeta_NKKU4y6XDq

11 Likes