r/ProgrammerHumor • • Jun 12 '25 • This 3

Meme andJavascriptForWeb

Post image
7.9k Upvotes

270 comments sorted by

View all comments

Show parent comments

0

u/nworld_dev Jun 12 '25

This is a bit more a function of static typing than OOP, but you're still correct.

It's also worth mentioning that static typing and well-defined OO structures allow for a much greater degree of compiler optimization, which is essentially a "for free" performance improvement.

For example, if you have an object in a highly-dynamic language, and you only ever reference name and id, behind the scenes it'll say "ok, pointer to each field to store it, we don't know what that field will be". Now every time you check that object you've splattered your cache, and the machine has no idea how big it'll be so it just guesses. Meanwhile in something like C, you can say "ok, this is a 16 character string, this is a two byte number, this is..." etc, and it knows exactly how big it is, so it can pack it in tightly without lots of memory & jumping about.

In addition, a lot of static analysis and simple optimizations can be done. For example, if you have a function that's x = 3; y = 5; z = x + y, that can get substituted before the program ever runs. If you have a function like x = a + b; z = m + n; o = x + z + i;, the compiler can figure out how to run a SIMD instruction and perform it all in one "chunk". Whereas in something like Javascript, that's several pointer jumps & dynamically evaluated. Now, lots of const and such can help with this, immutability does improve things because it fixes the type, but still the principle stands. Anything you can give the compiler to say "go wild with optimizing this" is a plus.

This doesn't sound like much but it adds up over a program. Computers keep getting faster and running on less power, but the memory's generally the big bottleneck of it all.

1

u/LickingSmegma Jun 12 '25

I already answered to another dude that it's not a function of static typing. Please stay with the program. PHP is not a statically-typed language.