Is this more optimized?

Post

I often find myself doing something similar to:

Instance.new("Folder", workspace).Name = "hi"

However this is unoptimized since the property is being modified after parenting, so I was wondering if this is any more optimized:

local function InstanceOneLiner(class : string, parent : Instance) : Instance
	local newObject : Instance = Instance.new(class)
	
	newObject.Changed:Once(function()
		newObject.Parent = parent
	end)
	
	return newObject
end

If not, is there any other way to make an optimized version?

Basic summarization of answer:
This:

local object = Instance.new(CLASS)
object.PROPERTY = VALUE
object.Parent = PARENT

Is the best because having a helper methods requires creation of temporary variables which gives more work to the garbage collector, and parenting it leads to Roblox handling internals along with physics update for things like parts and more.

https://devforum.roblox.com/t/psa-dont-use-instancenew-with-parent-argument/30296

This articles explains it all

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