Skip to content

Commit

Permalink
Significant performance improvements
Browse files Browse the repository at this point in the history
 * utils/clone uses a for-in loop instead of mapping over Object.keys.
 * State.prototype.clone, State.prototype.set, and State.prototype.get no
   longer perform unnecessary clones.
  • Loading branch information
wildlyinaccurate committed Mar 25, 2016
1 parent a73136b commit f6fe561
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 29 deletions.
20 changes: 12 additions & 8 deletions dist/plait.js

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

2 changes: 1 addition & 1 deletion dist/plait.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/plait.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/plait.min.js.map

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions lib/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var State = function () {
_createClass(State, [{
key: 'clone',
value: function clone() {
return new State(this.toObject());
return new State(this.obj);
}
}, {
key: 'toObject',
Expand All @@ -49,18 +49,22 @@ var State = function () {
}, {
key: 'set',
value: function set(prop, val) {
var obj = this.toObject();
var newObj = {};

obj[prop] = val;
newObj[prop] = val;

return new State(obj);
return new State(Object.assign({}, this.obj, newObj));
}
}, {
key: 'get',
value: function get(prop) {
var obj = this.toObject();
var val = this.obj[prop];

if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && !val.hasOwnProperty('@@Plait/State')) {
return (0, _clone2.default)(val);
}

return obj[prop];
return val;
}
}, {
key: 'update',
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.default = clone;
function clone(obj) {
var newObj = Array.isArray(obj) ? [] : {};

Object.keys(obj).map(function (k) {
for (var k in obj) {
var val = obj[k];

if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object') {
Expand All @@ -22,7 +22,7 @@ function clone(obj) {
} else {
newObj[k] = val;
}
});
}

return newObj;
}
16 changes: 10 additions & 6 deletions src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ class State {
}

clone () {
return new State(this.toObject())
return new State(this.obj)
}

toObject () {
return clone(this.obj)
}

set (prop, val) {
const obj = this.toObject()
const newObj = {}

obj[prop] = val
newObj[prop] = val

return new State(obj)
return new State(Object.assign({}, this.obj, newObj))
}

get (prop) {
const obj = this.toObject()
const val = this.obj[prop]

if (typeof val === 'object' && !val.hasOwnProperty('@@Plait/State')) {
return clone(val)
}

return obj[prop]
return val
}

update (prop, updater) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/clone.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function clone (obj) {
const newObj = Array.isArray(obj) ? [] : {}

Object.keys(obj).map(k => {
for (const k in obj) {
const val = obj[k]

if (typeof val === 'object') {
Expand All @@ -13,7 +13,7 @@ export default function clone (obj) {
} else {
newObj[k] = val
}
})
}

return newObj
}
5 changes: 4 additions & 1 deletion test/StateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ describe('State', () => {
})

it('is actually immutable', () => {
const obj = { a: 1 }
const nestedObj = { a: { nested: 1 } }
const obj = { a: 1, deep: nestedObj }
const state = new State(obj)

obj.a = 2
state.get('deep').a.nested = 2

expect(state.get('a')).toBe(1)
expect(state.get('deep').a.nested).toBe(1)
})

it('is deeply immutable', () => {
Expand Down

0 comments on commit f6fe561

Please sign in to comment.