[UPDATE: 2.0] SmartCreate. An easy way to create Instances

Could you post the code you did for running these tests?

1 Like

image
Creating instances 1000 times.

local Create = require(game.ServerScriptService:WaitForChild("SmartCreate")).Create

local start = os.clock()

for i = 1, 1000 do
	Create("Part", {
		Name = "Smart",
		Parent = workspace,
		Color = Color3.fromRGB(255,255,255),
		CFrame = CFrame.new(1,10,0),
		Anchored = true,
		CanCollide = true,
		Size = Vector3.new(2,10,2),

		Attributes = {
			["IsTouched"] = false,
			["DefaultSize"] = Vector3.one,
		},

		Touched = function(otherPart)
			print(otherPart.Name .. " touched!")
		end,

		Children = {
			Create("Part", {
				Name = "Head",
				Size = Vector3.new(5,5,5),
				Anchored = true
			})
		}
	})
end

print(os.clock() - start .. ": SmartCreate's time")

local start = os.clock()

for i = 1, 1000 do
	--Setting the property of part
	local Part = Instance.new("Part")
	Part.Name = "Smart"
	Part.Color = Color3.fromRGB(255,255,255)
	Part.CFrame = CFrame.new(1,10,0)
	Part.Anchored = true
	Part.CanCollide = true
	Part.Size = Vector3.new(2,10,2)

	--Setting the attributes of part 
	Part:SetAttribute("IsTouched", false)
	Part:SetAttribute("DefaultSize", Vector3.one)

	--Setting the Touched Event of the part
	Part.Touched:Connect(function(otherPart)
		print(otherPart.Name .. " touched!")
	end) 

	--Setting the child of the part
	local Head = Instance.new("Part")
	Head.Name = "Head"
	Head.Size = Vector3.new(5,5,5)
	Head.Anchored = true
	Head.Parent = Part

	Part.Parent = workspace
end

print(os.clock() - start .. ": Roblox's time")
1 Like

With smart create, you set the Parent early on. Try setting the variables in the same order for both and see if it makes a difference

1 Like

In the code for smart create parent is always set last.

3 Likes

Ok, if you reverse the order. I feel like the results are very inconsistent

code:

local Create = require(game.ReplicatedStorage.Packages:WaitForChild("SmartCreate")).Create

task.wait(5)
local start = os.clock()

for i = 1, 1000 do
	--Setting the property of part
	local Part = Instance.new("Part")
	Part.Name = "Smart"
	Part.Color = Color3.fromRGB(255,255,255)
	Part.CFrame = CFrame.new(1,10,0)
	Part.Anchored = true
	Part.CanCollide = true
	Part.Size = Vector3.new(2,10,2)

	--Setting the attributes of part 
	Part:SetAttribute("IsTouched", false)
	Part:SetAttribute("DefaultSize", Vector3.one)

	--Setting the Touched Event of the part
	Part.Touched:Connect(function(otherPart)
		print(otherPart.Name .. " touched!")
	end) 

	--Setting the child of the part
	local Head = Instance.new("Part")
	Head.Name = "Head"
	Head.Size = Vector3.new(5,5,5)
	Head.Anchored = true
	Head.Parent = Part

	Part.Parent = workspace
end

print(os.clock() - start .. ": Roblox's time")

task.wait(5)
local start = os.clock()

for i = 1, 1000 do
	Create("Part", {
		Name = "Smart",
		Parent = workspace,
		Color = Color3.fromRGB(255,255,255),
		CFrame = CFrame.new(1,10,0),
		Anchored = true,
		CanCollide = true,
		Size = Vector3.new(2,10,2),

		Attributes = {
			["IsTouched"] = false,
			["DefaultSize"] = Vector3.one,
		},

		Touched = function(otherPart)
			print(otherPart.Name .. " touched!")
		end,

		Children = {
			Create("Part", {
				Name = "Head",
				Size = Vector3.new(5,5,5),
				Anchored = true
			})
		},
	})
end

print(os.clock() - start .. ": SmartCreate's time")

1 Like

That’s why SmartCreate isn’t faster they are the same the difference is negligible.

2 Likes

I agree, but there is a significantly difference in just creating 1 instance.

1 Like

Ok, reverse that and Roblox’s time will be faster.

2 Likes

I am running it on 2 different scripts

1 Like

Ok, so I conclude the discussion by saying that SmartCreate and Instance.new() are pretty much the same in terms of speed. There are cases where SmartCreate is faster than Instance.new(), and vice versa.

The true intention of this module was to make a Lua version of @rbxts/altmake - npm. I disliked the idea of having multiple lines of code just to customize 1 instance. With SmartCreate I can use 1 function to bulk update an instance.

1 Like

Your module is already using instance.new internally

local Create = function(ClassName: string, Properties: Properties)
    local Object = Instance.new(ClassName)
    Edit(Object, Properties)

    return Object
end

There is thereby no way it can be any faster than Instance.new() Not only that, but the Edit function for changing the properties also has a lot of code that wouldn’t be ran normally.

If you want to speed up setting the same properties on lots of parts, you should instead use the clone method. That is faster when it comes to lots of properties.

3 Likes

I agree, I do not believe that SmartCreate is faster than Instance.new() since it uses Instance.new(). But when I just ran some random tests for just creating 1 instance, I found some how the SmartCreate was faster. Overall I have concluded this discussion here

1 Like

You guys care about picosecond gains in something that honestly should be taken for what it is: it looks less messy than Instance.new and property setting.

Some recommendations though:
Hold off the Parent variable until the for loop is closed in the Edit function. Parent should never be set before properties and events are ready.

Allow Properties.Children to expect Instance or Instance[]. This is really just a way to clean up curly brace hell for just a single property.

For events, automatically pass the Object itself. For example:
Touched: (object: Instance, hit: Instance) => void
Usually when events happen, the developer modifies the Instance in question. You cannot get that information from automatically hooked up things.

Automatically disconnect events for when the Instance is destroyed. QOL waiting for Roblox to just… do this themselves.

It’s cool to see yet another way of creating Instances, honestly ever since Fusion came out, these seem to of gotten way more popular. A while ago I made something like this for TypeScript, honestly didn’t know altmake even existed.

5 Likes

I wouldn’t have made it a big deal if it didn’t market itself as being faster lol although logically impossible.

This is already done if you actually check out the code.

Personally I wouldn’t use it but I could see why people might want to.

2 Likes

Yea, altmake was the main reason I wanted a Lua version. I usually use Roblox-ts, and when I had to use Lua for a project I missed the altmake functionality.

1 Like

No, it’s not. ^^

Even though Properties.Parent gets set as a variable it is not stopped from being set from the Properties for loop. There is no filter stopping it from being included in there.

It’s actually set twice.

3 Likes

It’s not set twice because it is removed from properties beforehand.
image

How so? The code doesn’t do anything differently, properties are set the same and everything as the normal method would go.

3 Likes

You’re right, I don’t know how I overlooked those lines. Honestly I read Properties on that line to be the Object itself, which didn’t make sense in my head

3 Likes

I can’t confirm that this is faster than roblox’s one, did a few runs using the Benchmarker plugin

this is the code ran (make sure it has the .bench at the end of the modulescript name)

--[[
This file is for use by Benchmarker (https://boatbomber.itch.io/benchmarker)

|WARNING| THIS RUNS IN YOUR REAL ENVIRONMENT. |WARNING|
--]]

return {
	ParameterGenerator = function()
		return
	end,

	Functions = {
		["SmartCreate"] = function(Profiler) 
			local Create = require(game.ServerScriptService:WaitForChild("SmartCreate")).Create

			local part = Create("Part", {
				Name = "Smart",
				Parent = workspace,
				Color = Color3.fromRGB(255,255,255),
				CFrame = CFrame.new(1,10,0),
				Anchored = true,
				CanCollide = true,
				Size = Vector3.new(2,10,2),

				Attributes = {
					["IsTouched"] = false,
					["DefaultSize"] = Vector3.one,
				},

				Touched = function(otherPart)
					print(otherPart.Name .. " touched!")
				end,

				Children = {
					Create("Part", {
						Name = "Head",
						Size = Vector3.new(5,5,5),
						Anchored = true
					})
				}
			})
			
			part:Destroy() -- so the workspace doesn't get messy
		end,

		["Roblox"] = function(Profiler) 
			--Setting the property of part
			local Part = Instance.new("Part")
			Part.Name = "Smart"
			Part.Color = Color3.fromRGB(255,255,255)
			Part.CFrame = CFrame.new(1,10,0)
			Part.Anchored = true
			Part.CanCollide = true
			Part.Size = Vector3.new(2,10,2)

			--Setting the attributes of part 
			Part:SetAttribute("IsTouched", false)
			Part:SetAttribute("DefaultSize", Vector3.one)

			--Setting the Touched Event of the part
			Part.Touched:Connect(function(otherPart)
				print(otherPart.Name .. " touched!")
			end) 

			--Setting the child of the part
			local Head = Instance.new("Part")
			Head.Name = "Head"
			Head.Size = Vector3.new(5,5,5)
			Head.Anchored = true
			Head.Parent = Part

			Part.Parent = workspace
			
			Part:Destroy()
		end,
	},
}

also I still wouldn’t use it as nobody cares if it’s a few μs faster as we’re in 2023 now, not in 1986


use the benchmarker plugin instead of desmos (it costs 18.36 AED but don’t worry about the price as it’s not too expensive (18.36 AED = 5 USD), also it’s on itch.io cuz boatbomber hates the plugin marketplace being toxic)

2 Likes

wow bro will i encounter 1k+ parts spawning irl?

Yet again. This resource shouldn’t be promoting that its faster than instance.new because even if it was, it does not matter if its a few microseconds faster and it uses instance.new internally so it cant be “faster”

2 Likes