Skip to content
This repository was archived by the owner on Aug 24, 2021. It is now read-only.

Commit 343a6c0

Browse files
mcollinahugomrdias
authored andcommitted
fix: use process.nextTick instead of setImmediate (#35)
I have see this specific setImmediate to cause unneeded latency in https://upload.clinicjs.org/public/1c86eeec6c2c4ee47e0d26d06cec62ee1fe74c745c610d5ba0a11bbe985649aa/31518.clinic-bubbleprof.html. This happens because other I/O activity happens between the hashing is completed and callback is executed. This change also removes the closure, having the added benefit of allowing `input` to be garbage collected if possible reducing memory pressure.
1 parent 7f9c6d1 commit 343a6c0

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"url": "https://github.com/multiformats/js-multihashing-async/issues"
3838
},
3939
"dependencies": {
40-
"async": "^2.6.1",
4140
"blakejs": "^1.1.0",
4241
"js-sha3": "^0.7.0",
4342
"multihashes": "~0.4.13",
@@ -63,6 +62,7 @@
6362
"Harlan T Wood <[email protected]>",
6463
"Juan Batiz-Benet <[email protected]>",
6564
"Marcin Rataj <[email protected]>",
65+
"Matteo Collina <[email protected]>",
6666
"Mitar <[email protected]>",
6767
"Pedro Teixeira <[email protected]>",
6868
"Richard Littauer <[email protected]>",

src/utils.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
'use strict'
22

3-
const setImmediate = require('async/setImmediate')
4-
53
exports.toCallback = (doWork) => {
64
return function (input, callback) {
7-
const done = (err, res) => setImmediate(() => {
8-
callback(err, res)
9-
})
10-
115
let res
126
try {
137
res = doWork(input)
148
} catch (err) {
15-
done(err)
9+
process.nextTick(callback, err)
1610
return
1711
}
1812

19-
done(null, res)
13+
process.nextTick(callback, null, res)
2014
}
2115
}
2216

0 commit comments

Comments
 (0)