Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I be using icepick.merge over lodash.merge

I've recently found a need for merging deeply inside my reducers.

I found that both icepick.merge and lodash.merge do the job.

It seems that icepick.merge is an efficient way merging and calling object.freeze recursively.

When working with react reducers (and immutability), when should you use icepick instead of lodash? Is it good practice to be calling object.freeze on arrays and objects?

like image 507
Tom Avatar asked Mar 15 '16 09:03

Tom


1 Answers

It is a matter of enforcing immutability or not, and what level of control / abstraction you want.

lodash

lodash gives you tools to handle data structures in a way that does not mutate them, but it does not enforce immutability on the data structure itself. E.g nothing is stopping you or any other developer from mutating state when using lodash.

icepick

icepick seems to let you keep your existing data structures but freeze them, allowing you to inspect the data structures in chrome and having them behave as they normally would. I have not used icepick in a project but i have noted that some people prefer it because of its lightweight nature and the fact that you keep the existing data structures, allowing them to be inspected in dev tools etc.

Immutable.js

My preferred way of solving this is to use ImmutableJS using the data structures that this library provides takes away the chore of freezing / thawing and enforces immutability consistently across your project. The data structures that immutable provides has built in API that is similar to that of regular javascript data-structures with the addition of some of the power tools from for example lodash, like deep merge.

like image 75
Jeger Avatar answered Sep 24 '22 14:09

Jeger