TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Immutability in JavaScript: A Contrarian View

4 pointsby sdesalasabout 5 years ago

3 comments

testbot123about 5 years ago
Immutability is used in JavaScript and especially React because it&#x27;s a core computer science concept with real-world benefits, not because it&#x27;s &quot;sexy&quot; or a fad.<p>Arguably the largest benefit in React-land is being able to pass around objects and do equality checks via reference. If I mutate an object and pass it as a prop down the tree, the consumer components won&#x27;t know that it&#x27;s been updated because React does shallow comparison by default (===). Meaning that wherever you have props in your code where you mutate an object, you have to write a function to determine equality, which leads to your code becoming bloated and unreadable, not to mention the massive loss in performance.<p>Mutability also harms our ability to test for expected outputs from given inputs. Mutations can cause side-effects that cause other mutations which can be difficult to see or predict. This means code cannot be accurately tested, and it becomes much more difficult to find bugs and near impossible to get a macro-level view of your application logic. In this sense, mutability can make your application &#x2F;more&#x2F; complex and difficult to maintain and extend.<p>Edit: Please read the other responses in that SO thread. They contain correct information while most of yours is misinformation&#x2F;speculation with an authoritative tone.
评论 #23246990 未加载
sdesalasabout 5 years ago
Sure dude. Have you ever used something other than React?
评论 #23248247 未加载
评论 #23248277 未加载
评论 #23248260 未加载
sdesalasabout 5 years ago
Feel free to pick a bone.