Skip to content

Commit

Permalink
Reorder some ops to avoid creating needless objects for no-op assignm…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
Petter Häggholm committed Nov 26, 2015
1 parent f10c961 commit 9562ddf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
Empty file added benchmark.js
Empty file.
29 changes: 21 additions & 8 deletions seamless-immutable.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
}

function arraySet(idx, value) {
if (idx in this) {
if (idx in this && this[idx] === value) {
return this;
}

Expand All @@ -102,12 +102,18 @@
var head = pth[0];

if (pth.length === 1) {
return arraySet.call(this, head, Immutable(value));
return arraySet.call(this, head, value);
} else {
var mutable = asMutableArray.call(this);
var tail = pth.slice(1);
// this[head] might (validly) be an (immutable) array or object
mutable[head] = this[head].setIn(tail, Immutable(value));
var newValue = this[head].setIn(tail, value);

if (head in this && this[head] === newValue) {
return this;
}

var mutable = asMutableArray.call(this);
mutable[head] = newValue;
return makeImmutableArray(mutable);
}
}
Expand Down Expand Up @@ -357,13 +363,20 @@
}

var tail = path.slice(1);
var mutable = quickCopy(this, this.instantiateEmptyObject());
if (mutable.hasOwnProperty(head) && mutable[head] !== undefined) {
var newValue;
if (this.hasOwnProperty(head) && this[head] !== undefined) {
// Might (validly) be object or array
mutable[head] = mutable[head].setIn(tail, Immutable(value));
newValue = this[head].setIn(tail, value);
} else {
mutable[head] = objectSetIn.call(immutableEmptyObject, tail, Immutable(value));
newValue = objectSetIn.call(immutableEmptyObject, tail, value);
}

if (this.hasOwnProperty(head) && this[head] === newValue) {
return this;
}

var mutable = quickCopy(this, this.instantiateEmptyObject());
mutable[head] = newValue;
return makeImmutableObject(mutable, this);
}

Expand Down
2 changes: 1 addition & 1 deletion seamless-immutable.production.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions src/seamless-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
}

function arraySet(idx, value) {
if (idx in this) {
if (idx in this && this[idx] === value) {
return this;
}

Expand All @@ -102,12 +102,18 @@
var head = pth[0];

if (pth.length === 1) {
return arraySet.call(this, head, Immutable(value));
return arraySet.call(this, head, value);
} else {
var mutable = asMutableArray.call(this);
var tail = pth.slice(1);
// this[head] might (validly) be an (immutable) array or object
mutable[head] = this[head].setIn(tail, Immutable(value));
var newValue = this[head].setIn(tail, value);

if (head in this && this[head] === newValue) {
return this;
}

var mutable = asMutableArray.call(this);
mutable[head] = newValue;
return makeImmutableArray(mutable);
}
}
Expand Down Expand Up @@ -357,13 +363,20 @@
}

var tail = path.slice(1);
var mutable = quickCopy(this, this.instantiateEmptyObject());
if (mutable.hasOwnProperty(head) && mutable[head] !== undefined) {
var newValue;
if (this.hasOwnProperty(head) && this[head] !== undefined) {
// Might (validly) be object or array
mutable[head] = mutable[head].setIn(tail, Immutable(value));
newValue = this[head].setIn(tail, value);
} else {
mutable[head] = objectSetIn.call(immutableEmptyObject, tail, Immutable(value));
newValue = objectSetIn.call(immutableEmptyObject, tail, value);
}

if (this.hasOwnProperty(head) && this[head] === newValue) {
return this;
}

var mutable = quickCopy(this, this.instantiateEmptyObject());
mutable[head] = newValue;
return makeImmutableObject(mutable, this);
}

Expand Down

0 comments on commit 9562ddf

Please sign in to comment.