Skip to content

Commit 4458e39

Browse files
committed
optimize nested case
1 parent 72f361b commit 4458e39

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

_tests/smoke.spec.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,40 @@ describe('memoize-proxy', () => {
3535

3636
const result1 = mm({a: 1, b: 2}, {extract: 'b', stuff: 1});
3737
const result2 = mm({a: '!', b: 2}, {extract: 'b', stuff: '!'});
38-
expect(result1).not.to.be.equal(result2);
39-
expect(result1).to.be.deep.equal(result2);
38+
expect(result1).to.be.equal(result2);
4039
expect(mm({a: 1, b: 2}, {extract: 'b', stuff: 1})).to.be.equal(mm({a: '!', b: 2}, {extract: 'b', stuff: '!'}));
4140
expect(mm({a: 1, b: 2}, {extract: 'b', stuff: 1})).to.be.equal(mm({a: '!', b: 2}, {extract: 'b', stuff: '!'}));
4241

43-
expect(mm({a: 1, b: 2}, {extract: 'b', stuff: 1})).not.to.be.deep.equal(mm({a: '!', b: 3}, {extract: 'b', stuff: '!'}));
44-
expect(mm({a: 1, b: 2}, {extract: 'b', stuff: 1})).not.to.be.deep.equal(mm({a: '!', b: 3}, {extract: 'a', stuff: '!'}));
42+
expect(mm({a: 1, b: 2}, {extract: 'b', stuff: 1})).not.to.be.deep.equal(mm({a: '!', b: 3}, {
43+
extract: 'b',
44+
stuff: '!'
45+
}));
46+
expect(mm({a: 1, b: 2}, {extract: 'b', stuff: 1})).not.to.be.deep.equal(mm({a: '!', b: 3}, {
47+
extract: 'a',
48+
stuff: '!'
49+
}));
50+
});
51+
52+
it('nested memoization returning value', () => {
53+
let callCount = 0;
54+
const mapStateToProps = (state) => ({
55+
a: state.a,
56+
state: state,
57+
callCount: callCount++
58+
});
59+
60+
const mm = memoize(memoize(mapStateToProps));
61+
const state1 = {a: 1};
62+
expect(mm(state1)).to.be.deep.equal({a: 1, state: state1, callCount: 0})
63+
expect(mm(state1)).to.be.deep.equal({a: 1, state: state1, callCount: 0})
64+
const state2 = {a: 2}
65+
expect(mm(state2)).to.be.deep.equal({a: 2, state: state2, callCount: 1})
66+
expect(mm(state2)).to.be.deep.equal({a: 2, state: state2, callCount: 1})
67+
const state3 = {a: 2, b: 3};
68+
expect(mm(state3)).to.be.deep.equal({a: 2, state: state2, callCount: 1})
69+
expect(mm(state2)).to.be.deep.equal({a: 2, state: state2, callCount: 1})
70+
71+
expect(mm(state1)).to.be.deep.equal({a: 1, state: state1, callCount: 2})
4572
});
4673

4774
it('memoize twice', () => {
@@ -133,11 +160,15 @@ describe('memoize-proxy', () => {
133160

134161
it('should pass name and content', () => {
135162
const fn = a => a;
136-
function func(a) { return a; }
163+
164+
function func(a) {
165+
return a;
166+
}
167+
137168
expect(memoize(fn).name).to.equal('fn');
138169
expect(memoize(a => a).name).to.equal('');
139170
expect(memoize(func).name).to.equal('func');
140-
expect(String(memoize(func))).to.equal('/* memoized by memoize-state */\n'+func);
171+
expect(String(memoize(func))).to.equal('/* memoized by memoize-state */\n' + func);
141172
});
142173

143174
it('should detect argument as result', () => {
@@ -156,22 +187,22 @@ describe('memoize-proxy', () => {
156187
const A = {
157188
data: 42
158189
};
159-
const B = { A };
160-
const C = { A };
190+
const B = {A};
191+
const C = {A};
161192

162193
let cache1, cache2;
163194

164195
const fn1 = ({A}) => {
165-
if(!cache1) {
166-
cache1=A;
196+
if (!cache1) {
197+
cache1 = A;
167198
} else {
168199
expect(cache1).to.be.equal(A);
169200
}
170201
};
171202

172203
const fn2 = ({A}) => {
173-
if(!cache2) {
174-
cache2=A;
204+
if (!cache2) {
205+
cache2 = A;
175206
} else {
176207
expect(cache2).not.to.be.equal(A);
177208
}
@@ -181,15 +212,15 @@ describe('memoize-proxy', () => {
181212
mfn1(B);
182213
mfn1(C);
183214

184-
const mfn2 = memoize(fn2, { nestedEquality: false });
215+
const mfn2 = memoize(fn2, {nestedEquality: false});
185216
mfn2(B);
186217
mfn2(C);
187218
});
188219

189220
it('smoke args memoization', () => {
190221
const o1 = {a: 1};
191222
const o2 = {a: 1};
192-
const f1 = memoize(obj => Object.assign({}, obj), { strictArity: true });
223+
const f1 = memoize(obj => Object.assign({}, obj), {strictArity: true});
193224
const f2 = memoize(obj => Object.assign({}, obj));
194225

195226
const result11 = f1(o1, 1, o1);

lib/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
1515

1616
/*eslint no-console: ["error", { allow: ["warn", "error"] }] */
1717

18+
var nothing = 'PROXY_EQUAL_NOTHING';
19+
1820
var emptyArray = [];
1921

2022
var defaultOptions = {
@@ -89,20 +91,21 @@ function deproxifyResult(result, affected, returnPureValue) {
8991
if (result.hasOwnProperty(i)) {
9092
var data = result[i];
9193
var newResult = deproxifyResult(data, affected, false);
92-
if (data && newResult) {
94+
if (data && newResult !== nothing) {
9395
altered = true;
96+
sub[i] = newResult;
97+
} else {
98+
sub[i] = data;
9499
}
95-
96-
sub[i] = newResult || data;
97100
}
98101
}
99102
if (altered) {
100103
return sub;
101104
}
102-
return returnPureValue && result;
105+
return returnPureValue ? result : nothing;
103106
}
104107

105-
return result;
108+
return returnPureValue ? result : nothing;
106109
}
107110

108111
function callIn(that, cache, args, func, memoizationDepth) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
},
4545
"homepage": "https://github.com/theKashey/memoize-state#readme",
4646
"dependencies": {
47-
"proxyequal": "^1.2.1"
47+
"proxyequal": "^1.2.3"
4848
}
4949
}

src/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010

1111
/*eslint no-console: ["error", { allow: ["warn", "error"] }] */
1212

13+
const nothing = 'PROXY_EQUAL_NOTHING';
14+
1315
const emptyArray = [];
1416

1517
const defaultOptions = {
@@ -79,20 +81,21 @@ function deproxifyResult(result, affected, returnPureValue) {
7981
if (result.hasOwnProperty(i)) {
8082
const data = result[i];
8183
const newResult = deproxifyResult(data, affected, false);
82-
if (data && newResult) {
84+
if (data && newResult !== nothing) {
8385
altered = true;
86+
sub[i] = newResult
87+
} else {
88+
sub[i] = data;
8489
}
85-
86-
sub[i] = newResult || data;
8790
}
8891
}
8992
if (altered) {
90-
return sub
93+
return sub;
9194
}
92-
return returnPureValue && result;
95+
return returnPureValue ? result : nothing;
9396
}
9497

95-
return result;
98+
return returnPureValue ? result : nothing;
9699
}
97100

98101
function callIn(that, cache, args, func, memoizationDepth, proxyMap = []) {
@@ -133,7 +136,7 @@ const equalHit = buildCompare(1);
133136
function transferProperties(source, target) {
134137
const keys = Object.getOwnPropertyNames(source);
135138

136-
for(const key of keys) {
139+
for (const key of keys) {
137140
const descriptor = Object.getOwnPropertyDescriptor(source, key);
138141
try {
139142
Object.defineProperty(target, key, descriptor);
@@ -201,7 +204,7 @@ function memoize(func, _options = {}) {
201204
args.length = Math.min(func.length, args.length);
202205
}
203206

204-
if(!options.nestedEquality){
207+
if (!options.nestedEquality) {
205208
proxyMap = {};
206209
}
207210

0 commit comments

Comments
 (0)