(The question is in the title)
type is a lua function that determines general coding types, like string or bool, and typeof is basically type with roblox instances built in. type(‘hi’) == “string” and typeof(Vector3.new()) == “Vector3”
I don’t think I understand the differences between the 2 …
Edit: So type checks what class it is and so does typeof?
typeof is basically a better version of type
it supports roblox items
e.g.
local myString = "hello there"
print(type(myString)) -- prints "string"
print(typeof(myString)) -- prints "string"
print(type(CFrame.new(1,1,1))) -- prints "userdata", since userdata means it is not any of the other lua types
print(typeof(CFrame.new(1,1,1))) -- prints "CFrame"
So you may as well only use typeof all the time as it does everything that type does but more?
Edit: so are there any uses that ONLY type can utilize and not typeof?
The only benefit to type
is that it’s considerably faster, or you can use it for identifying if something is non-vanilla Lua (if you needed to do that for some reason).
What do you mean ‘faster’? Does that mean it loads faster?
It just takes longer for typeof
to return, yeah. It used to be more significant, but here’s a current benchmark:
benchmark code
local type, typeof, reps = type, typeof, 1e7
local types = {"string", 1, true, game}
wait(5) -- waiting to allow game to load since obviously the one running while game loads would be slower
local t1 = tick()
for i = 1, reps do
local x = typeof(types[i % 4]) -- pick a "random" type to check (equal amount of each)
end
local t2 = tick()
wait(1) -- give it a little time in case there's any lingering effects (idk)
local t3 = tick()
for i = 1, reps do
local x = type(types[i % 4]) -- pick a "random" type to check
end
local t4 = tick()
print(
[[type:]], t4 - t3, [[
typeof:]], t2 - t1
)
… and the results:
(without trying to distinguish between types of userdata, it’s still slower:)
The difference used to be a lot less negligible, and both should be fine now, but it’s something to be aware of. If you don’t need to account for all of Roblox’s types of userdata differently, you should stick with type
.