Str:sub() or string.sub(str)

str:sub() or string.sub(str), which is better performance wise?

also #str or string.len(str)?

It depends. The former is shorter as the string is already inputted and you need less parameters. But, you also need a variable for that.

I don’t think this is correct syntax:

"string":sub() --or maybe it is, but it looks weird

As per #str or str.len, use the former as it’s shorter.

I don’t think they’re too different performance-wise, but they are different efficiency-wise.

There are different ways to achieve something in programming languages, so always choose the shortest/most efficient version!

Edit:
This is fine (with parentheses):

("string"):sub()

There’s no difference significant enough to be considered a difference, but calling functions directly from the string library is just a bit faster than method calls for the same purpose.

You can’t do that, but if you wrap it in parentheses it’s fine:

("bheyb"):sub(2, 4)
3 Likes

In Roblox Lua you should be using the long approach string.method() for all string methods. Recently it’s been made so this is optimized, but it seems the : version had not been, and probably won’t be.

For the length, though, # works.

2 Likes