Skip to content

Commit d8f8040

Browse files
hugomrdiasvmx
authored andcommitted
fix: reduce bundle size
BREAKING CHANGE: change from big.js to bignumber.js The impact of this change is only on the `snapshot` field of the stats, as those values are represented as Big Numbers.
1 parent 2c2246e commit d8f8040

File tree

8 files changed

+195
-37
lines changed

8 files changed

+195
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const snapshot = stats.snapshot
9393
console.log('stats: %j', snapshot)
9494
```
9595

96-
the snapshot will contain the following keys, with the values being [Big.js](https://github.com/MikeMcl/big.js#readme) instances:
96+
the snapshot will contain the following keys, with the values being [bignumber.js](https://github.com/MikeMcl/bignumber.js#readme) instances:
9797

9898
```js
9999
// stats: {

package.json

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
4444
"devDependencies": {
4545
"@nodeutils/defaults-deep": "^1.1.0",
46-
"aegir": "^17.1.1",
46+
"aegir": "^18.0.2",
4747
"benchmark": "^2.1.4",
4848
"chai": "^4.2.0",
4949
"dirty-chai": "^2.0.1",
@@ -56,36 +56,28 @@
5656
"lodash": "^4.17.11",
5757
"lodash.range": "^3.2.0",
5858
"lodash.without": "^4.4.0",
59-
"multiaddr": "^6.0.0",
6059
"ncp": "^2.0.0",
6160
"peer-book": "~0.9.0",
6261
"peer-id": "~0.12.0",
6362
"peer-info": "~0.15.0",
6463
"rimraf": "^2.6.2",
65-
"stats-lite": "^2.2.0"
64+
"stats-lite": "^2.2.0",
65+
"safe-buffer": "^5.1.2",
66+
"uuid": "^3.3.2"
6667
},
6768
"dependencies": {
6869
"async": "^2.6.1",
69-
"big.js": "^5.2.2",
70+
"bignumber.js": "^8.0.1",
7071
"cids": "~0.5.5",
7172
"debug": "^4.1.0",
7273
"ipfs-block": "~0.8.0",
73-
"lodash.debounce": "^4.0.8",
74-
"lodash.find": "^4.6.0",
75-
"lodash.groupby": "^4.6.0",
74+
"just-debounce-it": "^1.1.0",
7675
"lodash.isequalwith": "^4.4.0",
77-
"lodash.isundefined": "^3.0.1",
78-
"lodash.pullallwith": "^4.7.0",
79-
"lodash.sortby": "^4.7.0",
80-
"lodash.uniqwith": "^4.5.0",
81-
"lodash.values": "^4.3.0",
8276
"moving-average": "^1.0.0",
8377
"multicodec": "~0.2.7",
8478
"multihashing-async": "~0.5.1",
8579
"protons": "^1.0.1",
86-
"pull-defer": "~0.2.3",
8780
"pull-length-prefixed": "^1.3.1",
88-
"pull-pushable": "^2.2.0",
8981
"pull-stream": "^3.6.9",
9082
"varint-decoder": "~0.1.1"
9183
},

src/decision-engine/index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ const waterfall = require('async/waterfall')
66
const nextTick = require('async/nextTick')
77

88
const map = require('async/map')
9-
const debounce = require('lodash.debounce')
10-
const uniqWith = require('lodash.uniqwith')
11-
const find = require('lodash.find')
12-
const values = require('lodash.values')
13-
const groupBy = require('lodash.groupby')
14-
const pullAllWith = require('lodash.pullallwith')
9+
const debounce = require('just-debounce-it')
1510

1611
const Message = require('../types/message')
1712
const Wantlist = require('../types/wantlist')
1813
const Ledger = require('./ledger')
19-
const logger = require('../utils').logger
14+
const { logger, groupBy, pullAllWith, uniqWith } = require('../utils')
2015

2116
const MAX_MESSAGE_SIZE = 512 * 1024
2217

@@ -92,18 +87,18 @@ class DecisionEngine {
9287
this._tasks = []
9388
const entries = tasks.map((t) => t.entry)
9489
const cids = entries.map((e) => e.cid)
95-
const uniqCids = uniqWith(cids, (a, b) => a.equals(b))
96-
const groupedTasks = groupBy(tasks, (task) => task.target.toB58String())
90+
const uniqCids = uniqWith((a, b) => a.equals(b), cids)
91+
const groupedTasks = groupBy(task => task.target.toB58String(), tasks)
9792

9893
waterfall([
9994
(callback) => map(uniqCids, (cid, cb) => {
10095
this.blockstore.get(cid, cb)
10196
}, callback),
102-
(blocks, callback) => each(values(groupedTasks), (tasks, cb) => {
97+
(blocks, callback) => each(Object.values(groupedTasks), (tasks, cb) => {
10398
// all tasks have the same target
10499
const peer = tasks[0].target
105100
const blockList = cids.map((cid) => {
106-
return find(blocks, (b) => b.cid.equals(cid))
101+
return blocks.find(b => b.cid.equals(cid))
107102
})
108103

109104
this._sendBlocks(peer, blockList, (err) => {
@@ -212,11 +207,11 @@ class DecisionEngine {
212207
_cancelWants (ledger, peerId, entries) {
213208
const id = peerId.toB58String()
214209

215-
pullAllWith(this._tasks, entries, (t, e) => {
210+
this._tasks = pullAllWith((t, e) => {
216211
const sameTarget = t.target.toB58String() === id
217212
const sameCid = t.entry.cid.equals(e.cid)
218213
return sameTarget && sameCid
219-
})
214+
}, this._tasks, entries)
220215
}
221216

222217
_addWants (ledger, peerId, entries, callback) {

src/stats/stat.js

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

33
const EventEmitter = require('events')
4-
const Big = require('big.js')
4+
const Big = require('bignumber.js')
55
const MovingAverage = require('moving-average')
66

77
class Stats extends EventEmitter {

src/types/wantlist/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const sort = require('lodash.sortby')
3+
const { sortBy } = require('../../utils')
44
const Entry = require('./entry')
55

66
class Wantlist {
@@ -64,11 +64,7 @@ class Wantlist {
6464
}
6565

6666
sortedEntries () {
67-
return new Map(
68-
sort(Array.from(this.set.entries()), (o) => {
69-
return o[1].key
70-
})
71-
)
67+
return new Map(sortBy(o => o[1].key, Array.from(this.set.entries())))
7268
}
7369

7470
contains (cid) {

src/utils.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const debug = require('debug')
1111
*
1212
* @private
1313
*/
14-
exports.logger = (id, subsystem) => {
14+
const logger = (id, subsystem) => {
1515
const name = ['bitswap']
1616
if (subsystem) {
1717
name.push(subsystem)
@@ -24,3 +24,67 @@ exports.logger = (id, subsystem) => {
2424

2525
return logger
2626
}
27+
28+
const includesWith = (pred, x, list) => {
29+
let idx = 0
30+
const len = list.length
31+
while (idx < len) {
32+
if (pred(x, list[idx])) {
33+
return true
34+
}
35+
idx += 1
36+
}
37+
return false
38+
}
39+
40+
const uniqWith = (pred, list) => {
41+
let idx = 0
42+
const len = list.length
43+
const result = []
44+
let item
45+
46+
while (idx < len) {
47+
item = list[idx]
48+
if (!includesWith(pred, item, result)) {
49+
result[result.length] = item
50+
}
51+
idx += 1
52+
}
53+
return result
54+
}
55+
56+
const groupBy = (pred, list) => {
57+
return list.reduce((acc, v) => {
58+
const k = pred(v)
59+
60+
if (acc[k]) {
61+
acc[k].push(v)
62+
} else {
63+
acc[k] = [v]
64+
}
65+
return acc
66+
}, {})
67+
}
68+
69+
const pullAllWith = (pred, list, values) => {
70+
return list.filter(i => {
71+
return !includesWith(pred, i, values)
72+
})
73+
}
74+
75+
const sortBy = (fn, list) => {
76+
return Array.prototype.slice.call(list, 0).sort((a, b) => {
77+
const aa = fn(a)
78+
const bb = fn(b)
79+
return aa < bb ? -1 : aa > bb ? 1 : 0
80+
})
81+
}
82+
83+
module.exports = {
84+
logger,
85+
includesWith,
86+
uniqWith,
87+
groupBy,
88+
pullAllWith,
89+
sortBy
90+
}

src/want-manager/msg-queue.js

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

3-
const debounce = require('lodash.debounce')
3+
const debounce = require('just-debounce-it')
44

55
const Message = require('../types/message')
66
const logger = require('../utils').logger

test/utils.spec.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
const chai = require('chai')
4+
chai.use(require('dirty-chai'))
5+
const expect = chai.expect
6+
const { groupBy, uniqWith, pullAllWith, includesWith, sortBy } = require('../src/utils')
7+
8+
describe('utils spec', function () {
9+
it('grouby', (done) => {
10+
const list = [
11+
{ name: 'name1', score: 1 },
12+
{ name: 'name2', score: 1 },
13+
{ name: 'name3', score: 2 }
14+
]
15+
const actual = groupBy(p => p.score === 1 ? 'a' : 'b', list)
16+
17+
expect(actual).to.deep.equal({
18+
a: [
19+
{ name: 'name1', score: 1 },
20+
{ name: 'name2', score: 1 }
21+
],
22+
b: [{ name: 'name3', score: 2 }]
23+
})
24+
25+
done()
26+
})
27+
28+
it('pullAllWith', (done) => {
29+
var array = [{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }]
30+
31+
const actual = pullAllWith(
32+
(a, b) => (a.x === b.x && a.y === b.y),
33+
array,
34+
[{ x: 3, y: 4 }]
35+
)
36+
37+
expect(actual).to.deep.equal([{ x: 1, y: 2 }, { x: 5, y: 6 }])
38+
39+
done()
40+
})
41+
42+
it('uniqWith', (done) => {
43+
class T {
44+
constructor (id) {
45+
this.id = id
46+
}
47+
48+
equals (instance) {
49+
return instance.id === this.id
50+
}
51+
}
52+
const list = [new T(1), new T(1), new T(2)]
53+
54+
const r = uniqWith((a, b) => a.equals(b), list)
55+
56+
if (r[0].id === 1 && r[1].id === 2) {
57+
return done()
58+
}
59+
60+
return done(new Error('no match'))
61+
})
62+
63+
it('includesWith', (done) => {
64+
class T {
65+
constructor (id) {
66+
this.id = id
67+
}
68+
69+
equals (instance) {
70+
return instance.id === this.id
71+
}
72+
}
73+
const list = [new T(1), new T(2), new T(3)]
74+
75+
const r1 = includesWith((a, b) => a.equals(b), new T(2), list)
76+
const r2 = includesWith((a, b) => a.equals(b), new T(4), list)
77+
expect(r1).to.be.true()
78+
expect(r2).to.be.false()
79+
80+
done()
81+
})
82+
83+
it('sortBy', (done) => {
84+
const list = [
85+
{
86+
id: 3,
87+
name: 'b'
88+
},
89+
{
90+
id: 2,
91+
name: 'a'
92+
},
93+
{
94+
id: 1,
95+
name: 'c'
96+
}
97+
]
98+
99+
const groupedList1 = sortBy(o => o.name, list)
100+
const groupedList2 = sortBy(o => o.id, list)
101+
102+
expect(groupedList1).to.be.deep.equal([ { id: 2, name: 'a' },
103+
{ id: 3, name: 'b' },
104+
{ id: 1, name: 'c' } ])
105+
expect(groupedList2).to.be.deep.equal([ { id: 1, name: 'c' },
106+
{ id: 2, name: 'a' },
107+
{ id: 3, name: 'b' } ])
108+
109+
done()
110+
})
111+
})

0 commit comments

Comments
 (0)