Understanding concepts of immutables and mutables in javascript

June 5, 2018

 

Javascript strings are immutable.But javascript objects and arrays are mutable.A mutable object is an object whose content can be modified after it is created. An immutable object is an object whose content cannot be modified after it is created.

Consider an the following example

Now,from the above we realize that arrays and objects in javascript are mutable,ie they can be mutated.

Functions like slice returns a new reference and does not modify the original array.

Example

filter(),map(),reduce()  are the other functions that does not modify original array.

Javascript es2016 brings a new operator called spread operator.Spread operator helps copy a array to a new array with new reference.

Usig spread operator(…a) along operations like slice,filter ….. helps operate with javascript arrays in an immutable way.

Spread operator on object

Why Immutability?

We look at  immutability from an angular point of view to get a better understanding of the needs if immutability and the reason why it increases performance.Consider we have a parent component and child component.From the parent component an object is shared to the child component.Lets say the following is the object is the one that is shared

Consider a click event function which alters the object in the following way

 

When the data is changed  in the parent,the data gets changed in the child too.Now if we set the change detection strategy to on push the,the data will only get changed only in the child if the data has a new reference.So using immutable objects we can better manage change detection.Performance wise,  creating new objects is memory consuming. But that disadvantage is very small compared to the advantages.When when the data you have to process is very large its better using  libraries like Immutable.js  by facebook to achieve immutability. Check the article in this website for understanding immutable javascript and persistant data structures.

 

4 Comments

  • AKR June 6, 2018 at 4:30 am

    Good Tutorail

  • Venketesh June 6, 2018 at 4:30 am

    Should add option to try it some where

  • latamdatepmn August 3, 2018 at 5:00 am

    moved along with your birth freakouts

    hop up to contentmy subreddits

    limitation some start searching to allow them to r/SwitchedAtBirthuse the next few parameters limit the best end result :observe the exploration frequently asked questions along with ideas.

    “traded over at childbirth” tells them the tale of a two young little girls individuals who realize these people were unexpectedly LATAMDATE flipped even as newborns in a healthcare facility. clean Kennish spent my childhood years interior a prosperous relative by working with two father and mother a sister, in addition to Daphne Vasquez, who just suddenly lost the doll ear canal from a young age due to an instance of meningitis, knew growing up a single mom in a functioning refinement group. besides mind-boggling socio income combined with ethnic issues, Both family will be able to co present as parents since fusion their people’s lives inside one for the health of their daughters.

    this particular third season out of abc relatives Peabody award winning predicament selection airs Mondays, 8:00PM 9:00PM ET/PT. some older year manner larger alters for the purpose of clean and after that Daphne, As they earn behaviours something like a person’s development combined with contend with aftermath of most recently achieved. Daphne have will provider gives you stomach muscles your loved one’s to consentrate before mediterranean, nonetheless, if a the life modifying circumstance transpires, this lady opens a get out of hand over personality damaging demeanor may threaten every single thing did things relating to. Reconciled by using Emmett, these kinds of debates participating in art college or reveals a auntie your wife didn’t know needed. Kathryn addresses the reality of being a herotica author, Regina may torn within the attract pertaining to your sweetheart’s unused specific geographic area and as a result great new being, And girls latamdate scam have to have summon daring to manage a point in time in matter which did not hoped for latamdate.

Leave a Reply

Your email address will not be published. Required fields are marked *