Page 3 of 3

Re: Create a class without external libraries

Posted: Sun Feb 14, 2016 1:09 am
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

Re: Create a class without external libraries

Posted: Sun Feb 14, 2016 5:20 am
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.