"rawequal()" or "=="?

I’ve been learning and using Lua for years, and today I decided to go over the built-in functions. And there, I saw “raw” functions. Now I bet these impact performance in some way, but my questions are:

  • What is the difference between “rawequal()” and “==”?
  • Which one should I use?

i would use ==

1 Like

rawget, rawset and rawequal are mostly used when you have metatables as they dont fire metamethods.

3 Likes

So if I use them, for example, to see whether 1 is equal to 1 or not, how will they differ from the “==” operator on the performance side?

You’ll typically want to use the raw___ functions inside of metamethods. They don’t have much use outside of that, and as for performance, I’ve never seen any complaints with using equal symbols, so I don’t know if it’s worth comparing the performance of.

Unless you’re working with metatables, it’s better practice to use == whenever you’re checking if two values are equal.
You shouldn’t worry about the performance, as the difference is minimal.
If your game is lagging, it’s not because of the use of ==.

8 Likes

To elaborate on what everyone else is saying, the raw functions are used inside metamethods to prevent infinite recursion. Say you have a metamethod that fires when table1 is compared to any other table. If we were to attempt to determine whether table1 was equal to another table inside the __eq metamethod that was fired for the first comparison, it would fire again…and again…and so on until you crash or get an error about maximum re-entry/c-stack. The raw functions do not fire this metamethod.

1 Like

Thanks for the help. My game wasn’t lagging, I just asked to know :smiley:

It’s not just better practice, it’s also just correct if you’re dealing with anything more than primitives.

> print(Vector3.new() == Vector3.new())
true
> print(rawequal(Vector3.new(), Vector3.new()))
false
9 Likes

In most cases you’d want Vector3.new() to equal Vector3.new().
What would be the cases in which you don’t want to?

That’s my point. == is not just better practice, it’s also just correct when you’re not dealing with primitives.

7 Likes