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.