2

I'm currently reading Eloquent JavaScript, and I don't really understand the interest of using persistent data structures like indicated in this paragraph. If I get it right, we are using pure functions (methods?) in this example, as the this.move method returns a new VillageState object without affecting the state of the original VillageState.
This way all the objects created until the problem gets solved are stored somewhere in the RAM stack, right? Then, should this extra data storage slow down the program too?
I don't really understand how it might be simpler to understand than using mutable data in this case. So I'd be glad if some of you could clarify this out for me, thanks. And please correct me if I'm wrong somewhere!

Alan Kersaudy
  • 739
  • 7
  • 15
  • 1
    You are 100% correct, mutation is faster. Please notice that Javascript engines are optimizing a lot though and these operations are still very fast. The point of this type of programing is not to achive great speeds, but decrease debugging time. The opposite is "often hard to understand and thus easy to break." Often times speed is not a huge concern, but developer time (=money) is. – Max Sep 07 '20 at 16:20
  • @Max Immutable data does imply no side-effects though. That means you can write massively threaded software without worrying about locks. In the state heavy OO paradigms like that common in Java and C++ too many locks and complicated locking processes have a tendency to slow everything down to a crawl because it is hard to predict when data access will become co-dependent on each other such that you end up with hundreds of threads that do nothing because they are all waiting on a single thread to finish – slebetman Sep 08 '20 at 09:44
  • As is usual in the world of optimizations.. often you find yourself giving up some micro-optimization (not needing to copy data) to gain even more optimization at higher levels – slebetman Sep 08 '20 at 09:45

0 Answers0