1

I am already understand mutability of the javascript and main idea of the mutable objects, but now I wont to fashin my knowledge.

For example we need to store object with many properties. We have 2 variants:

  1. Create mutable complex js object and not mutate it.

    const object = { ... };
    const newObject = Object.assingn({}, object, {someProp: newValue});
    

    And then all old object will be not equal to newObject.

  2. Create Immutable object via some library, example with Immutable JS

    const object = Immutable.fromJS({...});
    Immutable.setIn(...)
    

    And we will get completely the same result, objects will be not equal.

I already know that Immutable does not deep copy of the objects, and all not modified data will reusable via links - is it the main idea? Will immutable js variant be more performance-friendly and faster?

Or it is only semanticly better, and better to have no ability to mutate object?

Dmitriy Kovalenko
  • 3,437
  • 3
  • 19
  • 39
  • "faster" depends on the use case. immutable.js lets you use "===" to quickly compare whole deep objects. if you need a lot of that sort of thing, it's fast. If you manually make new object containers, then "===" works in vanilla, and is just as fast; immutable.js just makes it "simpler" to manage such structures. Generally speaking, if your code is not complete crap or talking to 3rd-party complete crap, passed objects should not get altered anyway, but some devs like that "in writing". Remember also that `===` is only skin-deep. – dandavis Feb 01 '17 at 21:03

0 Answers0