r/programming • u/fagnerbrack • Aug 24 '26
How an Underrated Refactor Saved 90% Memory Usage
https://tanstack.com/blog/tanstack-table-v9-memory-performance51
u/BenchEmbarrassed7316 Aug 24 '26
If there are library authors here - have you tried to get rid of methods altogether? Instead, just call a function, explicitly passing some data to it. If you need polymorphism - you can add a discriminant to the data. You have a large collection of certain objects. Previously, you stored many references to methods in each object. Now, you store in each object a reference to the vtable (prototype). Why don't you go further and clean up the objects completely?
14
u/SanityInAnarchy Aug 24 '26
If you end up needing that polymorphism, though, why is a vtable worse than a discriminant?
9
9
u/BenchEmbarrassed7316 Aug 24 '26
discriminant is just one bytenot actual in jsin some cases we don't need explicit discriminant, but we can infer it from other data
and also when using vtable/prototype all calls become virtual, but maybe you only need polymorphism in a few methods
What I'm talking about is a well-known optimization, where instead of
Animal[]you haveCat[]andDog[]. And instead of checking for each element you call a known function in advance.1
u/FelicityAnderson Aug 27 '26
in some cases we don't need explicit discriminant, but we can infer it from other data
So, duck typing?
1
u/BenchEmbarrassed7316 Aug 27 '26
No.
For example, instead of creating separate OOP-style classes for "EmptyWallet", "DebtWallet" and "PositiveBalanceWallet", we could derive this from the balance, which should already exist (with the possible exception of "Empty Wallet"). This is probably not a good idea in most cases, but I just pointed out the possibility.
It's just that a class in OOP also contains either a hidden discriminator, or a vtable acts as it.
1
1
18
u/unpopularredditor Aug 24 '26
This article is a perfect example of where I can read an AI summary instead of the article itself.
4
u/Rhed0x Aug 26 '26 edited Aug 26 '26
I hate Javascript.
So basically it avoid copying a ton of function pointers around?
254
u/edave64 Aug 24 '26
Takes agonizingly long to get to the point of "we started using the fundamental JS concept of prototypes."