Create a class without external libraries

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Create a class without external libraries

Post by s-ol »

So I built a quick benchmark. Creating 200 tiny instances with classes built using closures with and without "self" in the closure and with metatables.

Gist: https://gist.github.com/S0lll0s/ac8eaf50d2124016242e

Results:

Code: Select all

$ luajit test.lua

testing the metatable-way
memory before: 30.0322265625
memory difference: 29.1962890625
---
testing the closure-way
memory before: 33.984375
memory difference: 48.9716796875
---
testing the closure-self-way
memory before: 34.69140625
memory difference: 42.73046875

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Create a class without external libraries

Post by Inny »

Kibita wrote:It's necessary to use the init function?
No, I just like to use an init function so that working with subclasses is easier. The alternative is to keep changing the metatable for an object as it passes through the new functions.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 8 guests