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

Commit

Permalink
fix: use process.nextTick instead of setImmediate (#35)
Browse files Browse the repository at this point in the history
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.
mcollina authored and hugomrdias committed Jan 8, 2019
1 parent 7f9c6d1 commit 343a6c0
Showing 2 changed files with 3 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
"url": "https://github.com/multiformats/js-multihashing-async/issues"
},
"dependencies": {
"async": "^2.6.1",
"blakejs": "^1.1.0",
"js-sha3": "^0.7.0",
"multihashes": "~0.4.13",
@@ -63,6 +62,7 @@
"Harlan T Wood <[email protected]>",
"Juan Batiz-Benet <[email protected]>",
"Marcin Rataj <[email protected]>",
"Matteo Collina <[email protected]>",
"Mitar <[email protected]>",
"Pedro Teixeira <[email protected]>",
"Richard Littauer <[email protected]>",
10 changes: 2 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
'use strict'

const setImmediate = require('async/setImmediate')

exports.toCallback = (doWork) => {
return function (input, callback) {
const done = (err, res) => setImmediate(() => {
callback(err, res)
})

let res
try {
res = doWork(input)
} catch (err) {
done(err)
process.nextTick(callback, err)
return
}

done(null, res)
process.nextTick(callback, null, res)
}
}

0 comments on commit 343a6c0

Please sign in to comment.