The Basics Of Basic Optimization

Wasn’t all of your data pre-calculated in that? That would probably mean that the slowest bit is just looking up data and setting properties; which would make sense that doing what you did made it faster.

For code which does lots of calculations, (e.g. inverse kinematics, physics simulation, etc.) BlueTaslem’s points make a lot more sense as they affect the speed much more.

1 Like

The data is, but the actual things like the UDim2s aren’t. About 64 are created per 1/60 of a second, so storing the index for UDim2.new does prevent a lot of those calls.

2 Likes

Generally speaking, you don’t want to optimize for things that aren’t a problem.
The usual workflow I’ve seen is to write code/features, run a profiler and work specifically on the problematic areas.

In roblox, the tools for this are somewhat lacking and poorly documented (something I’d like to fix in the near future), but the Micro Profiler will get you an idea of what’s taking the most time.

It’s usually not anything lua related.

5 Likes

Some tips I found online ( some don’t apply to ROBLOX ):

1 Like

Are these two ways of setting up a local function just as effective as each other?

local This = function()
---stuff
end
local function This()
---stuff
end

Both are defining local variables to be that function, so yes.

2 Likes

Okay that’s good, thanks for making this tutorial.

This times a bunch.

There is a difference.

local This = function()
	print(This)
end
local function This()
	print(This)
end
local This; This = function()
	print(This)
end

The first prints nil; this will not work as a recursive function. The second two print the function and can be used recursively.

10 Likes

What gives better performance?

local function OnPlayerAdded(player)
    print(player.Name)
end

game:GetService("Players").PlayerAdded:Connect(OnPlayerAdded)

or

game:GetService("Players").PlayerAdded:Connect(function(player)
    print(player.Name)
end)

Theoretically the second one? Since you are skipping the variable declaration. But that’s a micro optimization.

Yeah but the second one creates a function for every time the PlayerAdded event is called, or am I wrong.

If I’m right wouldn’t it be better to create a function first and then call it?

No? You’re only passing one function.

It’s only a micro optimization if you only :Connect() once to the anonymous function.
Not to mention creating anonymous functions that are created when scripts first run won’t make a difference other than a few milliseconds or something (huge scripts loading in VM), if you are doing iterations with anonymous functions it could be a problem as you are recreating the anonymous function every frame or step and at that point you could just declare a function in place of anonymous functions and pass that for micro optimization in most cases, in some its a huge optimization really depends on what you’re trying to achieve in the end especially when not all declared functions will work and you may have to use anonymous functions for expected behavior (ran into this a couple times this past month).

Yes, not creating a new function each iteration, and instead reusing it, would be better. I’m saying for a single connection, defining a function may be overkill.

1 Like