:Clone() is actually slower than creating a new instance directly. How come?

I’ve conducted a test in which I create an instance (in my example I did a Part) and set its properties once, then store it in a variable. So we have something like this:

local part = Instance.new("Part")
part.BrickColor =  BrickColor.new("Really red")
part.Transparency = .5

In one test, I used the following code:

for i = 1, 100 do
local new = part:Clone()
part.Parent = workspace
end

And then:

for i = 1, 100 do
local part = Instance.new("Part")
part.BrickColor =  BrickColor.new("Really red")
part.Transparency = .5
part.Parent = workspace
end

To my surprise, the loop that clones the existing instance is actually significantly behind how fast the bottom loop ran. I compared tick() calls to get an accurate representation of how long both executions took.

In my mind’s eye, it would make sense for :Clone() to actually be the more ideal method, because of the fact I saved one part in memory with properties all set and ready to go. But apparently, something happens behind the scenes I’m not aware of.

Any insight appreciated, thanks!

3 Likes

Look. Creating new its just simple function to create something.
But. Then we cloning something model or part, it takes more time to create in memory and then spawn with seted parent.

Oh and here’s an error in your code.

local new = part:Clone()
part.Parent = workspace -- should be new.Parent

How significantly slower is it? Could you provide more data

2 Likes

Try this test with way more property changes, it is likely that :Clone() tries to clone every single property and not the ones that were changed as .new may just create a new object which its properties are already preset.

It is only a guess however as I can’t find out what’s behind the scenes.

1 Like

With a longer iteration, I think 300 parts being made, it ended up being about 2 tenths of a second i think

I don’t entirely remember.

Yeah it is slower, the best thing you should do if you need to create something in quick succession is to make an object pooling system, store all the items in the workspace and CFrame them to a far away location when not being used.

1 Like