Which way should I define the workspace?

Which way should I reference the workspace in my script? Everyone is saying different ways.

game.Workspace
workspace
game:GetService("Workspace")
1 Like
workspace

But why

game.Workspace is referencing the workspace by its name, so if you were to change its name for whatever reason, all your scripts containing game.Workspace will error.

game:GetService("Workspace") and workspace are the same; both will reference the workspace no matter what you name it, so just choose the shorter one for convenience and readability.
It will always reference the workspace no matter what name you choose for it.

cause it’s the best shorthand for it, if you are renaming the service (workspace) within the explorer then u must get it with :GetService

local s = tick()
for i = 0, 1e7 do
	local a = game:GetService("Workspace")
end
print("Get Service: ", tick()-s)

local s = tick()
for i = 0, 1e7 do
	local a = game.Workspace
end
print("Indexing: ", tick()-s)

local s = tick()
for i = 0, 1e7 do
	local a = workspace
end
print("Direct: ", tick()-s)

:GetService() is the slowest, it takes 0.867 seconds if you run it 10,000,000 times
game.Workspace takes 0.522 seconds if you run it 10,000,000 times
workspace takes 0.014 seconds if you run it 10,000,000 times

Using workspace is more than 50 times faster

5 Likes

that’s true for most services but Workspace is a property of game, so game.Workspace won’t error even if its name isn’t “Workspace”

1 Like

Just do whatevers shortest to save urself the most time. I just use workspace

both work theoretically, but workspace is both the cleanest and performance-efficient

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.