Skip to content

Commit 9f818b4

Browse files
hugomrdiasdirkmc
authored andcommitted
fix: reduce size (#203)
change lodash.isequalwith for a custom much smaller function specific for this use case. 72.22KB > 67.87KB gzipped
1 parent 8b16b80 commit 9f818b4

File tree

5 files changed

+103
-14
lines changed

5 files changed

+103
-14
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
module.exports = {
4-
bundlesize: { maxSize: '210kB' },
4+
bundlesize: { maxSize: '68kB' },
55
karma: {
66
files: [{
77
pattern: 'test/test-data/**/*',

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"debug": "^4.1.0",
7777
"ipfs-block": "~0.8.0",
7878
"just-debounce-it": "^1.1.0",
79-
"lodash.isequalwith": "^4.4.0",
8079
"moving-average": "^1.0.0",
8180
"multicodec": "~0.5.0",
8281
"multihashing-async": "^0.8.0",

src/types/message/index.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
const protons = require('protons')
44
const Block = require('ipfs-block')
5-
const isEqualWith = require('lodash.isequalwith')
65
const CID = require('cids')
76
const codecName = require('multicodec/src/name-table')
87
const vd = require('varint-decoder')
98
const multihashing = require('multihashing-async')
10-
9+
const { isMapEqual } = require('../../utils')
1110
const pbm = protons(require('./message.proto'))
1211
const Entry = require('./entry')
1312

@@ -106,15 +105,9 @@ class BitswapMessage {
106105
}
107106

108107
equals (other) {
109-
const cmp = (a, b) => {
110-
if (a.equals && typeof a.equals === 'function') {
111-
return a.equals(b)
112-
}
113-
}
114-
115108
if (this.full !== other.full ||
116-
!isEqualWith(this.wantlist, other.wantlist, cmp) ||
117-
!isEqualWith(this.blocks, other.blocks, cmp)
109+
!isMapEqual(this.wantlist, other.wantlist) ||
110+
!isMapEqual(this.blocks, other.blocks)
118111
) {
119112
return false
120113
}

src/utils.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,43 @@ const sortBy = (fn, list) => {
8080
})
8181
}
8282

83+
/**
84+
* Is equal for Maps of BitswapMessageEntry or Blocks
85+
* @param {Map} a
86+
* @param {Map} b
87+
* @returns {boolean}
88+
*/
89+
const isMapEqual = (a, b) => {
90+
if (a.size !== b.size) {
91+
return false
92+
}
93+
94+
for (const [key, valueA] of a) {
95+
if (!b.has(key)) {
96+
return false
97+
}
98+
99+
const valueB = b.get(key)
100+
101+
// Support BitswapMessageEntry
102+
if (typeof valueA.equals === 'function' && !valueA.equals(valueB)) {
103+
return false
104+
}
105+
// Support Blocks
106+
if (valueA._data && !valueA._data.equals(valueB._data)) {
107+
return false
108+
}
109+
}
110+
111+
return true
112+
}
113+
83114
module.exports = {
84115
logger,
85116
includesWith,
86117
uniqWith,
87118
groupBy,
88119
pullAllWith,
89-
sortBy
120+
sortBy,
121+
isMapEqual
90122
}

test/utils.spec.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
/* eslint-env mocha */
22
'use strict'
33
const chai = require('chai')
4+
const CID = require('cids')
5+
const Block = require('ipfs-block')
6+
const { Buffer } = require('buffer')
7+
const multihashing = require('multihashing-async')
8+
const BitswapMessageEntry = require('../src/types/message/entry')
9+
410
chai.use(require('dirty-chai'))
511
const expect = chai.expect
6-
const { groupBy, uniqWith, pullAllWith, includesWith, sortBy } = require('../src/utils')
12+
const { groupBy, uniqWith, pullAllWith, includesWith, sortBy, isMapEqual } = require('../src/utils')
713

814
describe('utils spec', function () {
915
it('groupBy', () => {
@@ -100,4 +106,63 @@ describe('utils spec', function () {
100106
{ id: 2, name: 'a' },
101107
{ id: 3, name: 'b' }])
102108
})
109+
110+
describe('isMapEqual', () => {
111+
it('should on be false when !== size', () => {
112+
expect(isMapEqual(
113+
new Map([['key1', 'value1'], ['key2', 'value2']]),
114+
new Map([['key1', 'value1']])
115+
)).to.be.false()
116+
})
117+
118+
it('should on be false if one key is missing', () => {
119+
expect(isMapEqual(
120+
new Map([['key1', 'value1'], ['key2', 'value2']]),
121+
new Map([['key1', 'value1'], ['key3', 'value2']])
122+
)).to.be.false()
123+
})
124+
125+
it('should on be false if BitswapMessageEntry dont match', async () => {
126+
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
127+
const cid1 = new CID(1, 'dag-pb', hash1)
128+
129+
expect(isMapEqual(
130+
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 2, true)]]),
131+
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 1, true)]])
132+
)).to.be.false()
133+
})
134+
135+
it('should on be true if BitswapMessageEntry match', async () => {
136+
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
137+
const cid1 = new CID(1, 'dag-pb', hash1)
138+
139+
expect(isMapEqual(
140+
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 1, true)]]),
141+
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 1, true)]])
142+
)).to.be.true()
143+
})
144+
145+
it('should on be false if Blocks dont match', async () => {
146+
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
147+
const cid1 = new CID(1, 'dag-pb', hash1)
148+
const block1 = new Block(Buffer.from('hello world'), cid1)
149+
const block2 = new Block(Buffer.from('hello world 2'), cid1)
150+
151+
expect(isMapEqual(
152+
new Map([['key1', block1], ['key2', block1]]),
153+
new Map([['key1', block1], ['key2', block2]])
154+
)).to.be.false()
155+
})
156+
157+
it('should on be true if Blocks match', async () => {
158+
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
159+
const cid1 = new CID(1, 'dag-pb', hash1)
160+
const block1 = new Block(Buffer.from('hello world'), cid1)
161+
162+
expect(isMapEqual(
163+
new Map([['key1', block1], ['key2', block1]]),
164+
new Map([['key1', block1], ['key2', block1]])
165+
)).to.be.true()
166+
})
167+
})
103168
})

0 commit comments

Comments
 (0)