This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

[Metatables] What's faster/more efficient?

Started by Sewbacca, 02 January 2017 - 09:58 PM
Sewbacca #1
Posted 02 January 2017 - 10:58 PM
Two methods to get the same result, what's faster (Not creation, just access speed)?

(Pseudo code)

{
  5 = object,
  6 = object,
  7 = object,
  8 = object,
  9 = object,
}

(Pseudo code)

{}, metatable:
{
  __index = function(_, i)
	if i >= 5 and <= 9 then
	  return object
	end
  end
}
Edited on 02 January 2017 - 09:59 PM
Exerro #2
Posted 02 January 2017 - 11:25 PM
The first is faster. It's a table index, rather than a table index, another table index (the metatable), and a function call.

Seeing as it's just 5 indexes, you can essentially ignore the memory difference.

You can just benchmark these kind of things yourself btw.
KingofGamesYami #3
Posted 03 January 2017 - 12:09 AM
metatables in general are slower than any other way of doing a certain task. They only save memory, and not much unless you plan to have a lot of "instances" of an "object".
Sewbacca #4
Posted 03 January 2017 - 12:17 AM
Thanks guys =)