Simple optimization, will remove if there isn't a way

local Map = Folder:FindFirstChild("Map")
if Map then
	Map.Parent = StoredMaps:FindFirstChild(Identifier) -- ignore identifier
end

This might be a really dumb question, but is there a better way to write this? I remember seeing one shorter that looked more efficient.

There really isn’t much you can, or should do, to optimize this. This is pretty basic code - you are literally just checking if it exists, and if it does, changing its parent. You can’t tweek it much, nor should you.

1 Like

Earlier in the script, make a variable for FindFirstChild.

local FindFirstChild = game.FindFirstChild

This caches the function.

You then get this for your optimized code:

local Map = Folder:FindFirstChild("Map")
if Map then
	Map.Parent = FindFirstChild(Parent, Identifier) -- ignore identifier
end

That’s as much as you can do. It’s not even worth bothering though, this is such a small optimization.

Don’t do this. Lookup caching in Lua should only be done on members you’re accessing repeatedly. This example in Lua would actually be less performant as it adds an extra allocation, yet still only does a single field lookup.

Roblox doesn’t use Lua anymore, and Luau has some quirks that actually make lookup caching an anti-optimization. Field access is inline cached, but Luau has an even more performant optimization specific to table:method() calls. (not table.method() calls however). You can read more about that here.

1 Like

That’s incredibly good to know, I appreciate the insight on that!

1 Like