Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 9562ddf

Browse files
author
Petter Häggholm
committed
Reorder some ops to avoid creating needless objects for no-op assignments
1 parent f10c961 commit 9562ddf

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

benchmark.js

Whitespace-only changes.

seamless-immutable.development.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
}
9090

9191
function arraySet(idx, value) {
92-
if (idx in this) {
92+
if (idx in this && this[idx] === value) {
9393
return this;
9494
}
9595

@@ -102,12 +102,18 @@
102102
var head = pth[0];
103103

104104
if (pth.length === 1) {
105-
return arraySet.call(this, head, Immutable(value));
105+
return arraySet.call(this, head, value);
106106
} else {
107-
var mutable = asMutableArray.call(this);
108107
var tail = pth.slice(1);
109108
// this[head] might (validly) be an (immutable) array or object
110-
mutable[head] = this[head].setIn(tail, Immutable(value));
109+
var newValue = this[head].setIn(tail, value);
110+
111+
if (head in this && this[head] === newValue) {
112+
return this;
113+
}
114+
115+
var mutable = asMutableArray.call(this);
116+
mutable[head] = newValue;
111117
return makeImmutableArray(mutable);
112118
}
113119
}
@@ -357,13 +363,20 @@
357363
}
358364

359365
var tail = path.slice(1);
360-
var mutable = quickCopy(this, this.instantiateEmptyObject());
361-
if (mutable.hasOwnProperty(head) && mutable[head] !== undefined) {
366+
var newValue;
367+
if (this.hasOwnProperty(head) && this[head] !== undefined) {
362368
// Might (validly) be object or array
363-
mutable[head] = mutable[head].setIn(tail, Immutable(value));
369+
newValue = this[head].setIn(tail, value);
364370
} else {
365-
mutable[head] = objectSetIn.call(immutableEmptyObject, tail, Immutable(value));
371+
newValue = objectSetIn.call(immutableEmptyObject, tail, value);
366372
}
373+
374+
if (this.hasOwnProperty(head) && this[head] === newValue) {
375+
return this;
376+
}
377+
378+
var mutable = quickCopy(this, this.instantiateEmptyObject());
379+
mutable[head] = newValue;
367380
return makeImmutableObject(mutable, this);
368381
}
369382

seamless-immutable.production.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/seamless-immutable.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
}
9090

9191
function arraySet(idx, value) {
92-
if (idx in this) {
92+
if (idx in this && this[idx] === value) {
9393
return this;
9494
}
9595

@@ -102,12 +102,18 @@
102102
var head = pth[0];
103103

104104
if (pth.length === 1) {
105-
return arraySet.call(this, head, Immutable(value));
105+
return arraySet.call(this, head, value);
106106
} else {
107-
var mutable = asMutableArray.call(this);
108107
var tail = pth.slice(1);
109108
// this[head] might (validly) be an (immutable) array or object
110-
mutable[head] = this[head].setIn(tail, Immutable(value));
109+
var newValue = this[head].setIn(tail, value);
110+
111+
if (head in this && this[head] === newValue) {
112+
return this;
113+
}
114+
115+
var mutable = asMutableArray.call(this);
116+
mutable[head] = newValue;
111117
return makeImmutableArray(mutable);
112118
}
113119
}
@@ -357,13 +363,20 @@
357363
}
358364

359365
var tail = path.slice(1);
360-
var mutable = quickCopy(this, this.instantiateEmptyObject());
361-
if (mutable.hasOwnProperty(head) && mutable[head] !== undefined) {
366+
var newValue;
367+
if (this.hasOwnProperty(head) && this[head] !== undefined) {
362368
// Might (validly) be object or array
363-
mutable[head] = mutable[head].setIn(tail, Immutable(value));
369+
newValue = this[head].setIn(tail, value);
364370
} else {
365-
mutable[head] = objectSetIn.call(immutableEmptyObject, tail, Immutable(value));
371+
newValue = objectSetIn.call(immutableEmptyObject, tail, value);
366372
}
373+
374+
if (this.hasOwnProperty(head) && this[head] === newValue) {
375+
return this;
376+
}
377+
378+
var mutable = quickCopy(this, this.instantiateEmptyObject());
379+
mutable[head] = newValue;
367380
return makeImmutableObject(mutable, this);
368381
}
369382

0 commit comments

Comments
 (0)