Nikolai Golub
Nikolai Golub

Reputation: 3567

Why 2 immutable.js Lists is deeply different?

I couldn't deeply compare 2 Lists, using simple deep object comparison.

Consider following code:

import {fromJS} from 'immutable';
import {describe, it} from 'mocha';
import { expect } from 'chai';

const o = {a: 123};
const A = fromJS([o]);
const B = fromJS([]).push(fromJS(o));

describe('Check', () => {
  it('should be deep equal', () => {
    expect(A).to.deep.equal(B);
  });
});

Why Immutable.js collections actually have state?

I saw libraries like chai-immutable, but I would like to understand purpose of this behavior?

Upvotes: 0

Views: 105

Answers (1)

juvian
juvian

Reputation: 16068

Immutable.js uses a state to make it efficient, so that when you add something to an immutable list, the references will be different but it won´t actually copy everything and be O(n).

As stated in their Github :

These data structures are highly efficient on modern JavaScript VMs by using structural sharing via hash maps tries and vector tries as popularized by Clojure and Scala, minimizing the need to copy or cache data.

Upvotes: 1

Related Questions