Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Which method is more optimized?

I'm trying to keep things as optimized as possible, and I was wondering if BrickColor.Black() is faster than BrickColor.new("Black"). Do any of you know?

2 comments

0

Don't do either -- they return constants

1

Why the hell is this downvoted. The man wants to know the answer.

2 answers

✓ Accepted answer

Don't do either. Don't recompute something when you know the answer in advance.

local BLACK = BrickColor.new("Black")

Using BLACK will involve no work at all, in comparison to BrickColor.foo() which involves (1) looking up BrickColor in the global table, (2) looking up whatever method you're calling in the BrickColor table, and (3) calling that function.


Do less work. Two things which are basically the same don't make a difference. Stop obsessing over "optimizing" code in this way. The real gains are from better algorithms.

If you're testing for efficiency, try logging tick() before and after execution.

Here's an example of the kind of code you would write to test the BrickColor speeds:

a = tick()
c = BrickColor.Black()
a = tick() - a

b = tick()
d = BrickColor.new("Black")
b = tick() - b

print("A = " .. a)
print("B = " .. b)

This returns

A = 4.7683715820313e-006
B = 5.9604644775391e-006

So BrickColor.Black() is indeed faster than BrickColor.new("Black")

This discussion is preserved as it was. New answers, comments, and votes are closed.