Immutable.js reference equality
given the following trivial code:
const Immutable = require('immutable');
const a = Immutable.fromJS({
a: 1,
b: [2, 3, 4],
c: {
d: 1
}
});
const b = a.setIn(['c', 'd'], "Something else");
const c = b.setIn(['c', 'd'], 1);
console.log(a.equals(b)); // true
console.log(Immutable.is(a, c)); // true
console.log(a === c); // false?
And for the final comparison I'd expect it to return true since I'm setting the path ['c', 'd']
to something else and then back to the original value, and with structural sharing I would expect that it results in c
holding a reference to the original data structure?
Do I mis-understand how this works?
immutable.js
add a comment |
given the following trivial code:
const Immutable = require('immutable');
const a = Immutable.fromJS({
a: 1,
b: [2, 3, 4],
c: {
d: 1
}
});
const b = a.setIn(['c', 'd'], "Something else");
const c = b.setIn(['c', 'd'], 1);
console.log(a.equals(b)); // true
console.log(Immutable.is(a, c)); // true
console.log(a === c); // false?
And for the final comparison I'd expect it to return true since I'm setting the path ['c', 'd']
to something else and then back to the original value, and with structural sharing I would expect that it results in c
holding a reference to the original data structure?
Do I mis-understand how this works?
immutable.js
add a comment |
given the following trivial code:
const Immutable = require('immutable');
const a = Immutable.fromJS({
a: 1,
b: [2, 3, 4],
c: {
d: 1
}
});
const b = a.setIn(['c', 'd'], "Something else");
const c = b.setIn(['c', 'd'], 1);
console.log(a.equals(b)); // true
console.log(Immutable.is(a, c)); // true
console.log(a === c); // false?
And for the final comparison I'd expect it to return true since I'm setting the path ['c', 'd']
to something else and then back to the original value, and with structural sharing I would expect that it results in c
holding a reference to the original data structure?
Do I mis-understand how this works?
immutable.js
given the following trivial code:
const Immutable = require('immutable');
const a = Immutable.fromJS({
a: 1,
b: [2, 3, 4],
c: {
d: 1
}
});
const b = a.setIn(['c', 'd'], "Something else");
const c = b.setIn(['c', 'd'], 1);
console.log(a.equals(b)); // true
console.log(Immutable.is(a, c)); // true
console.log(a === c); // false?
And for the final comparison I'd expect it to return true since I'm setting the path ['c', 'd']
to something else and then back to the original value, and with structural sharing I would expect that it results in c
holding a reference to the original data structure?
Do I mis-understand how this works?
immutable.js
immutable.js
asked Jan 19 at 23:44
KevinKevin
16.6k1175129
16.6k1175129
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First, this one console.log(a.equals(b));
returns false actually:
Now for your question, as documented in Immutable.js here at "Return self on no-op optimization" sub-chapter:
When possible, Immutable.js avoids creating new objects for updates
where no change in value occurred, to allow for efficient reference
equality checking to quickly determine if no change occurred.
There's that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 2);
updatedMap === originalMap; // No-op .set() returned the original reference.
However updates which do result in a change will return a new
reference. Each of these operations occur independently, so two
similar updates will not return the same reference:
And that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 1000);
// New instance, leaving the original immutable.
updatedMap !== originalMap;
const anotherUpdatedMap = originalMap.set('b', 1000);
// Despite both the results of the same operation, each created a new reference.
anotherUpdatedMap !== updatedMap;
// However the two are value equal.
anotherUpdatedMap.equals(updatedMap);
Since you are changing the value, setIn
returns a new reference. Therefore they are not equal by reference.
Hope I helped :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272331%2fimmutable-js-reference-equality%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, this one console.log(a.equals(b));
returns false actually:
Now for your question, as documented in Immutable.js here at "Return self on no-op optimization" sub-chapter:
When possible, Immutable.js avoids creating new objects for updates
where no change in value occurred, to allow for efficient reference
equality checking to quickly determine if no change occurred.
There's that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 2);
updatedMap === originalMap; // No-op .set() returned the original reference.
However updates which do result in a change will return a new
reference. Each of these operations occur independently, so two
similar updates will not return the same reference:
And that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 1000);
// New instance, leaving the original immutable.
updatedMap !== originalMap;
const anotherUpdatedMap = originalMap.set('b', 1000);
// Despite both the results of the same operation, each created a new reference.
anotherUpdatedMap !== updatedMap;
// However the two are value equal.
anotherUpdatedMap.equals(updatedMap);
Since you are changing the value, setIn
returns a new reference. Therefore they are not equal by reference.
Hope I helped :)
add a comment |
First, this one console.log(a.equals(b));
returns false actually:
Now for your question, as documented in Immutable.js here at "Return self on no-op optimization" sub-chapter:
When possible, Immutable.js avoids creating new objects for updates
where no change in value occurred, to allow for efficient reference
equality checking to quickly determine if no change occurred.
There's that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 2);
updatedMap === originalMap; // No-op .set() returned the original reference.
However updates which do result in a change will return a new
reference. Each of these operations occur independently, so two
similar updates will not return the same reference:
And that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 1000);
// New instance, leaving the original immutable.
updatedMap !== originalMap;
const anotherUpdatedMap = originalMap.set('b', 1000);
// Despite both the results of the same operation, each created a new reference.
anotherUpdatedMap !== updatedMap;
// However the two are value equal.
anotherUpdatedMap.equals(updatedMap);
Since you are changing the value, setIn
returns a new reference. Therefore they are not equal by reference.
Hope I helped :)
add a comment |
First, this one console.log(a.equals(b));
returns false actually:
Now for your question, as documented in Immutable.js here at "Return self on no-op optimization" sub-chapter:
When possible, Immutable.js avoids creating new objects for updates
where no change in value occurred, to allow for efficient reference
equality checking to quickly determine if no change occurred.
There's that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 2);
updatedMap === originalMap; // No-op .set() returned the original reference.
However updates which do result in a change will return a new
reference. Each of these operations occur independently, so two
similar updates will not return the same reference:
And that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 1000);
// New instance, leaving the original immutable.
updatedMap !== originalMap;
const anotherUpdatedMap = originalMap.set('b', 1000);
// Despite both the results of the same operation, each created a new reference.
anotherUpdatedMap !== updatedMap;
// However the two are value equal.
anotherUpdatedMap.equals(updatedMap);
Since you are changing the value, setIn
returns a new reference. Therefore they are not equal by reference.
Hope I helped :)
First, this one console.log(a.equals(b));
returns false actually:
Now for your question, as documented in Immutable.js here at "Return self on no-op optimization" sub-chapter:
When possible, Immutable.js avoids creating new objects for updates
where no change in value occurred, to allow for efficient reference
equality checking to quickly determine if no change occurred.
There's that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 2);
updatedMap === originalMap; // No-op .set() returned the original reference.
However updates which do result in a change will return a new
reference. Each of these operations occur independently, so two
similar updates will not return the same reference:
And that example:
const { Map } = require('immutable');
const originalMap = Map({ a: 1, b: 2, c: 3 });
const updatedMap = originalMap.set('b', 1000);
// New instance, leaving the original immutable.
updatedMap !== originalMap;
const anotherUpdatedMap = originalMap.set('b', 1000);
// Despite both the results of the same operation, each created a new reference.
anotherUpdatedMap !== updatedMap;
// However the two are value equal.
anotherUpdatedMap.equals(updatedMap);
Since you are changing the value, setIn
returns a new reference. Therefore they are not equal by reference.
Hope I helped :)
answered Jan 20 at 0:33
SomoKRoceSSomoKRoceS
512414
512414
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272331%2fimmutable-js-reference-equality%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown