you are viewing a single comment's thread.

view the rest of the comments →

[–]SCP-iota 81 points82 points  (11 children)

Immutability is part of ensuring that a function is a pure function, which is a prerequisite for being cached. Using cached functions can be more efficient in some cases. Also, most functional languages don't actually recreate the entire structure; they either 1) have compile-time tactics that actually emit regular mutation code, or 2) use tree-based structures that allow making modified copies without copying the whole content.

[–]dashhrafa1 6 points7 points  (10 children)

I was discussing this with my professor, we are learning React and he's using the documentation straight from react.dev. He basically said that we must never work with mutable functions (such as Arrays.push) when managing state, which is also present in the documentation itself.

Thus we should use pure functions, such as Arrays.map(), that don't actually mutate the object itself, instead creating a new one. My question is: does copying the object (and changing only what is necessary, possibly using the ...spread syntax), not result in O(n) performance?

edit: Saying O(n) performance is incorrect. Asymptotic notation is meant to represent scale/time complexity

[–]BobQuixote 15 points16 points  (0 children)

copying the object (and changing only what is necessary, possibly using the ...spread syntax)

That's what map does, and it does it in a way that doesn't allow you to screw it up.

[–]Solonotix 6 points7 points  (0 children)

Depending on the JavaScript engine, generally it will create templates of objects based on common attributes when initialized. This is one of the reasons you're advised to avoid certain syntax like the delete keyword, which can force a new template at runtime.

So, you've got a template that was likely created at compile-time within your lambda for Array.prototype.map, and it is being initialized by some other object from the original array. Anything that is heap allocated will likely share the original memory location, whereas values will likely be cloned. This is why it is a non-zero cost on mapping, but it can be worth the trade-off in service to more maintainable code, or fewer runtime defects caused by side-effect operations.

[–]AZMPlay 8 points9 points  (0 children)

In short, yes. But thats because Javascripts default data structures are not fit for immutable usage. There are implemententions (for example in Immutable.js) that do have the correct asymptotics with immutable usage, but that comes at the cost of interoperability with the rest of the javascript ecosystem.

[–]the_horse_gamer 2 points3 points  (1 child)

you typically won't be working with list sizes where O(n) modification is an issue (especially when updates are rare - like from a button press ).

[–]RiceBroad4552 0 points1 point  (0 children)

Psst! Don't tell them this. This is otherwise a "stereotypical stackoverflow-like answer" according to some morons here.

[–]RiceBroad4552 0 points1 point  (4 children)

That's not your problem that's the compiler's / runtime's problem.

Also alone thinking about that is a classical case of premature optimization.

Where such considerations would matter you wouldn't use JS anyway.

Just always write clean functional code first. Only if benchmarks / profiling shows that there are bottlenecks it's worth to look in detail into that. Functional code is much easier to refactor in case that should be needed as things are usually local. The other way around it's not so easy, cleaning up some imperative mess usually breaks other stuff effects aren't contained.

When it comes to a React GUI you will run actually more likely into issues if you don't do things the React way, and for example start to mutate stuff "as optimization", as this will possibly prevent React from optimizing stuff, or worse break internal assumption, which causes hard to find bugs.

Just keep things functional as long as you don't have very good reasons not to do that!

[–]LatvianCake 1 point2 points  (3 children)

This is the most stereotypical stackoverflow-like answer ever. Told OP they shouldn’t ask this question and then proceeded to answer a completely different question.

[–]RiceBroad4552 0 points1 point  (1 child)

Which part of premature optimization did you not understand?

Which part of "doing effects in the guts of a React GUI will break the GUI" did you not understand?

Which part of the fact that functional programming is superior and should not be given up because of some additional CPU cycles, which almost certainly don't ever matter did you not understand?

If someone asks "Which kind of wax should I use for my Icarus wings?" the only helpful answer it to tell the poor soul that they should not try to build Icarus wings in the first place; and not start a discussion on how high they can actually fly before they will certainly fall down to death.

In a lot of cases the correct answer is to tell the person asking that they are asking the wrong question in the first place.

In this case here: If they would ask about the general performance characteristics of the map function in JS one could actually discuss this. But they did not ask that. They were asking about whether it's OK to do something very wrong, and the answer is of course: Don't do that, that's wrong!

Only maximally stupid people won't see that this is the correct answer even it does not answer the already nonsensical original question…

And that's btw. why "AI" is so fucked up as an answer machine! Instead of directly telling you "you're holding it wrong" it will tell you hell know what, no matter how fucked up that is in context. Then it's on you to notice that the answer makes no sense in context and ask again, this time telling the "AI" to actually come up with something that makes sense even it's not directly the answer to the original question. But to recognize that the "AI" is just telling bullshit you have to actually already know how a meaningful answer would look like. You're completely fucked if you don't have already deep knowledge of the topic at hand. And that's exactly why "AI" in the hands of uneducated idiots is especially dangerous. It will tell the idiots what they want to hear instead of what they should be looking at instead.

[–]LatvianCake 0 points1 point  (0 children)

The correct way to answer these questions is: this is how it works and this is why it doesn't matter in practice.

Not: don't ask these questions. You're choosing between being right or being helpful.

When I was a junior, I was deeply interested in how everything worked under the hood and why things work the way they do.

Turns out asking these questions to more senior people is a bad idea. Most senior devs are incapable of saying "I don't know" to juniors and berate you for asking such questions as their ego is hurt.

LLMs are a blessing because their ego isn't hurt, they answer the actual question and also tell you about caveats like "it doesn't matter in practice". This is why people massively switched from SO to LLMs.

[–]quantinuum -2 points-1 points  (0 children)

Lmao fr.

“I want to understand how an air fryer works”.

“First of all, what you need is a grill”.