Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is Best .toJS() vs .toJSON() in immutable js?

I am new to React+Redux+Immutable js ,I am using toJS() to tranforming my Immutable Data structure to Native javascript form.But Recently I stumbleupon with this tweet

Author of Immutable.Js

Tweet says .toJS() is very expensive So can i go with toJSON() https://facebook.github.io/immutable-js/docs/#/List/toJSON is it ok with perf issue

UPDATE

const initialState = fromJS({
   postArr:[]
});

    const mapStateToProps = (state) => {
        return{
            posts:state.allPosts.toJS()
        }
    };

Pls provide some example with my own Reducer

How can i iterate this.props.posts without using to.js()

like image 811
Nane Avatar asked Dec 08 '22 17:12

Nane


1 Answers

From what I understand from the documentation:

  • toJSON() returns a shallowly copied version of the immutable object (not a JSON string, although the name might suggest otherwise), which should only be used to pass to JSON.stringify() (in other words: you shouldn't use it as a regular object in your code).

  • toJS() returns a deeply copied version of the immutable object, which you can subsequently use/modify in your code. Because it's a deeply copied version, it's more expensive to generate.

like image 77
robertklep Avatar answered Jan 06 '23 12:01

robertklep