Skip to content

Instantly share code, notes, and snippets.

@guilhermebruzzi
Last active April 16, 2024 17:46
Show Gist options
  • Save guilhermebruzzi/eb633cf532be20292df0c9744ac09c7c to your computer and use it in GitHub Desktop.
Save guilhermebruzzi/eb633cf532be20292df0c9744ac09c7c to your computer and use it in GitHub Desktop.
Remove Immutable JS from your source code

I decided to remove all Immutable code on our big project because it wasn't causing our React to render less (using selectors on our redux layer was actually better).

Since our project has tests to check if everything is still working, I was able to achieve that with the following steps:

Search on all files on vscode or any editor with Use Regular Expression option (command+alt+r on vscode):

(Immutable|\.(size|count\(|toJS(?!\w)|fromJS(?!\w)|first[\(\)]|get\(|set\(|findEntry\(|getIn\(|setIn\(|contains\(|delete\(|asImmutable|add\())

Then on each find that was actually from a Immutable code:

  • Remove Immutable imports
  • Replaced size or count with length
  • Removed toJs, fromJS and asImmutable
  • Replaced first, get and getIn with lodash get: https://lodash.com/docs/4.17.15#get
  • Replaced set by first copying the object with lodash cloneDeep: https://lodash.com/docs/4.17.15#cloneDeep and then actually assigning direct values on the copyed object
  • Replaced setIn with same approach of set but verifying manually on a if statement if object has the property that allows me to alter it's value (example: if (o && o.a) { o.a.b = newValue })
  • Replaced findEntry with find from javascript
  • Replaced delete with splice from javascript
  • Replaced add on a Set to just adding to an Array and then using lodash uniq: https://lodash.com/docs/4.17.15#uniq

Also Check reduces with 3 params (Immutable Maps reduce) by searching with: \.reduce\(\([\w\s]+,[\w\s]+,[\w\s]+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment