Persistent data structures,trie and immutable js
interview questions , Javascript / June 18, 2018

If you do not know about immutability read more here http://truetocode.com/understanding-concepts-of-immutables-and-mutables-in-javascript/378/. Immutable data is never changed once it is created.It can prevent unwanted alteration of data and helps us in more efficient programming.Mutability may cause unwanted bugs as the data may get changed accidentally. Consider we have an array that contains the name of fruits say,  

Now consider that  for some reason we want to push the name of an Car into the array and do some thing with it.For that we call the pushCar method . After that  we want to  push the name of a fruit into the array and do some thing with it using the pushFruit method. In the push fruit method we expect the array to only contain fruits but our array has the name of a car since it was mutated. See the below code to get better understanding

  We can achieve our requirement using spread operator as the spread operator copies the parent a

But,when we have a large data set this is very expensive as spread operator copies the parent and copying is very expensive. To tackle this problem and make the array of fruits not to change every…

Share this page in social media platforms