Comments (11)
You are correct that icepick
is in the first camp. One thing to note, though, is that there are no "instances" -- icepick only returns plain (but frozen) objects that can be used with anything that doesn't try to modify them (direct property access, arr.map(), etc..). seamless-immutable
is not quite in the second camp -- it still returns plain objects, but adds a few more methods to them for convenience.
If you want mutable data back out, they way to get it is to deeply clone the object. You can use something like Lodash _.cloneDeep
or even JSON.parse(JSON.stringify(obj))
.
Would a i.thaw()
function that does this be helpful?
from icepick.
Having an api would help, thaw
sounds like a good name.
I just realize once we freeze an object there is no unfreeze, we can only clone it to a new one. It would seem that thaw
then mutate then freeze
again will have roughly the same performance as diffing (eg. assoc
etc.) the immutable itself (which does the same thing)?
If so, perhaps we should just diff the immutable itself, and avoid thaw
most of the time.
from icepick.
What do you mean by "diff the immutable"? The proper way to see if things have changed would be to see if objects are still reference-equal to their previous values.
from icepick.
My wording was probably wrong, sorry about that. I simply meant something like newColl = i.assoc(coll, "b", 3)
. And I was talking about how icepick
would be updating b
in this case.
If I understand correctly:
- Say
coll
is frozen already, when you are creating thenewColl
, you have to copycoll
's value into a mutable object again, and then changeb
, and then freeze it again. - But unlike the initial
i.freeze(coll)
, we don't have to freeze any data recursively, only the object that has changed, ie. the object containingb
. - So this is faster than
i.thaw(coll); coll.b = 3; i.freeze(coll);
particularly for largercoll
, because we don't need to freeze other part of it.
True?
If so then we shouldn't use thaw
/freeze
for mutation, we should try to change coll
into a newColl
with assoc
, which makes thaw
less desirable.
TL;DR: i.thaw
is nice to have as a helper function, but probably shouldn't be used for mutation.
from icepick.
Yep, I think you understand. If you look at the implementation of i.assoc
that's essentially what it does -- (shallow)copy, modify, freeze.
from icepick.
I have been using icepick in production for a month now and things are working great, though I did find i.thaw
to be a useful addition, imagine this scenario:
Let say we are using a virtual-dom rendering system that allow template to be rendered both server-side and client-side. Now imagine we are to render a list of tweets.
Because data from backend are just normal object, we are doing something like this:
var tweets = tweets.map(function(tweet, i) {
return tweetView(tweet);
});
Say we have a variable that's global, but is needed when rendering tweetView:
var domain = 'twitter.com';
var tweets = tweets.map(function(tweet, i) {
tweet.domain = domain;
return tweetView(tweet);
});
On server-side this is fine, but on client-side our model is an icepick-powered store, which means tweet.domain = domain;
will fail.
At this point we have a few options:
- Make server use icepick as well, which doesn't make much sense as data is used only once.
- Do an Object.isForzen check, and set data differently. This works but expose icepick api to virtual-dom template, which feel kinda weird.
i.thaw
, modify and pass the data as normal object. Unfortunately we will take a performance penalty here from clone.
Do you see any better approach?
from icepick.
Could you do something like:
var domain = 'twitter.com';
var tweetViews = tweets.map(function(tweet, i) {
return tweetView(i.assoc(tweet, "domain", domain));
});
The update to the tweet would be discarded, but it would work. The other option would be to update all your tweets with it:
var tweets = i.map(tweets, function (tweet) {
return i.assoc(tweet, "domain", domain);
});
// and then
var tweetViews = tweets.map(function(tweet, i) {
return tweetView(tweet);
});
from icepick.
One thing I am not sure about: on server-side my tweets
is a plain array that's not frozen, does i.map
and i.assoc
still work?
from icepick.
Yes. It'll work with any plain JS Array or Object, and they don't have to be frozen.
from icepick.
Wow cheers, would i.assoc
actually be faster than i.thaw
in my use case though? Both of them do clone
but i.thaw
will have to do it recursively.
from icepick.
Yes, assoc
would be faster since it's a shallow clone.
from icepick.
Related Issues (20)
- Is it by design array method like I.push(arr, data) doesn't freeze data HOT 1
- Missing dissocIn. HOT 7
- Doc on github readme wrong lol HOT 2
- icepick.merge() of two identical objects containing an array returns a new object HOT 4
- Documentation unclear about whether icepick adds methods to `Object.prototype` HOT 3
- Add `thru` method to `chain` wrapper HOT 1
- mergeIn HOT 2
- Using icepick.js in browser directly? HOT 11
- `assign` description incorrect? HOT 3
- A typo in README
- Tag is missing for 2.0.0 HOT 1
- Uglify fails on 2.0 with RCA HOT 2
- icepick/fp interface HOT 1
- icepick chain fails with map and filter HOT 2
- Please add a LICENSE file HOT 2
- merge doesn't work with objects that don't have a prototype. HOT 2
- Main entrypoint not compatible with ES5 HOT 3
- Generic "in" operator HOT 1
- Version of Icepick that does not freeze HOT 2
- Order changed by updateIn
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from icepick.