Skip to content

Commit

Permalink
fix: reduce size (#203)
Browse files Browse the repository at this point in the history
change lodash.isequalwith for a custom much smaller function specific for this use case.

72.22KB > 67.87KB gzipped
  • Loading branch information
hugomrdias authored and dirkmc committed Dec 11, 2019
1 parent 8b16b80 commit 9f818b4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

module.exports = {
bundlesize: { maxSize: '210kB' },
bundlesize: { maxSize: '68kB' },
karma: {
files: [{
pattern: 'test/test-data/**/*',
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"debug": "^4.1.0",
"ipfs-block": "~0.8.0",
"just-debounce-it": "^1.1.0",
"lodash.isequalwith": "^4.4.0",
"moving-average": "^1.0.0",
"multicodec": "~0.5.0",
"multihashing-async": "^0.8.0",
Expand Down
13 changes: 3 additions & 10 deletions src/types/message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

const protons = require('protons')
const Block = require('ipfs-block')
const isEqualWith = require('lodash.isequalwith')
const CID = require('cids')
const codecName = require('multicodec/src/name-table')
const vd = require('varint-decoder')
const multihashing = require('multihashing-async')

const { isMapEqual } = require('../../utils')
const pbm = protons(require('./message.proto'))
const Entry = require('./entry')

Expand Down Expand Up @@ -106,15 +105,9 @@ class BitswapMessage {
}

equals (other) {
const cmp = (a, b) => {
if (a.equals && typeof a.equals === 'function') {
return a.equals(b)
}
}

if (this.full !== other.full ||
!isEqualWith(this.wantlist, other.wantlist, cmp) ||
!isEqualWith(this.blocks, other.blocks, cmp)
!isMapEqual(this.wantlist, other.wantlist) ||
!isMapEqual(this.blocks, other.blocks)
) {
return false
}
Expand Down
34 changes: 33 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,43 @@ const sortBy = (fn, list) => {
})
}

/**
* Is equal for Maps of BitswapMessageEntry or Blocks
* @param {Map} a
* @param {Map} b
* @returns {boolean}
*/
const isMapEqual = (a, b) => {
if (a.size !== b.size) {
return false
}

for (const [key, valueA] of a) {
if (!b.has(key)) {
return false
}

const valueB = b.get(key)

// Support BitswapMessageEntry
if (typeof valueA.equals === 'function' && !valueA.equals(valueB)) {
return false
}
// Support Blocks
if (valueA._data && !valueA._data.equals(valueB._data)) {
return false
}
}

return true
}

module.exports = {
logger,
includesWith,
uniqWith,
groupBy,
pullAllWith,
sortBy
sortBy,
isMapEqual
}
67 changes: 66 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const CID = require('cids')
const Block = require('ipfs-block')
const { Buffer } = require('buffer')
const multihashing = require('multihashing-async')
const BitswapMessageEntry = require('../src/types/message/entry')

chai.use(require('dirty-chai'))
const expect = chai.expect
const { groupBy, uniqWith, pullAllWith, includesWith, sortBy } = require('../src/utils')
const { groupBy, uniqWith, pullAllWith, includesWith, sortBy, isMapEqual } = require('../src/utils')

describe('utils spec', function () {
it('groupBy', () => {
Expand Down Expand Up @@ -100,4 +106,63 @@ describe('utils spec', function () {
{ id: 2, name: 'a' },
{ id: 3, name: 'b' }])
})

describe('isMapEqual', () => {
it('should on be false when !== size', () => {
expect(isMapEqual(
new Map([['key1', 'value1'], ['key2', 'value2']]),
new Map([['key1', 'value1']])
)).to.be.false()
})

it('should on be false if one key is missing', () => {
expect(isMapEqual(
new Map([['key1', 'value1'], ['key2', 'value2']]),
new Map([['key1', 'value1'], ['key3', 'value2']])
)).to.be.false()
})

it('should on be false if BitswapMessageEntry dont match', async () => {
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
const cid1 = new CID(1, 'dag-pb', hash1)

expect(isMapEqual(
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 2, true)]]),
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 1, true)]])
)).to.be.false()
})

it('should on be true if BitswapMessageEntry match', async () => {
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
const cid1 = new CID(1, 'dag-pb', hash1)

expect(isMapEqual(
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 1, true)]]),
new Map([['key1', new BitswapMessageEntry(cid1, 1, true)], ['key2', new BitswapMessageEntry(cid1, 1, true)]])
)).to.be.true()
})

it('should on be false if Blocks dont match', async () => {
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
const cid1 = new CID(1, 'dag-pb', hash1)
const block1 = new Block(Buffer.from('hello world'), cid1)
const block2 = new Block(Buffer.from('hello world 2'), cid1)

expect(isMapEqual(
new Map([['key1', block1], ['key2', block1]]),
new Map([['key1', block1], ['key2', block2]])
)).to.be.false()
})

it('should on be true if Blocks match', async () => {
const hash1 = await multihashing(Buffer.from('OMG!1'), 'sha2-256')
const cid1 = new CID(1, 'dag-pb', hash1)
const block1 = new Block(Buffer.from('hello world'), cid1)

expect(isMapEqual(
new Map([['key1', block1], ['key2', block1]]),
new Map([['key1', block1], ['key2', block1]])
)).to.be.true()
})
})
})

0 comments on commit 9f818b4

Please sign in to comment.