-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use native promisify utility when possible #49
Conversation
"naive" performance testing: const sut = require('./index')
const iterations = 10000
const sleep = (fn) => { setTimeout(fn, 100) }
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
const native = sut(sleep)
const pify = sut(sleep, { FORCE_DISABLE_NATIVE: true }) // option added for performance testing
async function timed(fn){
const hrstart = process.hrtime()
await Promise.all([...Array(iterations).keys()].map(() => fn() )) // simulate `iteration` sleeps
const hrend = process.hrtime(hrstart)
return hrend[1]/1000000
}
async function test() {
const pifyResults = await Promise.all([...Array(100).keys()].map(() => timed(pify)))
const nativeResults = await Promise.all([...Array(100).keys()].map(() => timed(native)))
console.log(`Native Average Execution: ${average(nativeResults)} ms`)
console.log(`Pify Average Execution: ${average(pifyResults)} ms`)
}
test() Results:
Native is a consistent winner between test runs. Note that this is a bit of an extreme test however. The current test is spinning up 10,000 promises that each sleep for 100 ms. This process is repeated 100 times and the average execution time is calculated. |
|
||
const supportsPromisify = typeof promisify === 'function'; | ||
const canUseNativePromisify = opts => { | ||
return supportsPromisify && opts.promiseModule === Promise && opts.errorFirst && !opts.multiArgs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do all these with util.promisify
using https://nodejs.org/api/util.html#util_custom_promisified_functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can, but you don't gain the performance benefits of util.promisify
. The underlying implementation would still be the pify method with a bit more setup required.
Current:
if (isCustom) {
return getPifyImplementation(fn);
}
Using custom:
if (isCustom) {
const promiseFn = return getPifyImplementation(fn);
fn[util.promisify.custom] = promiseFn;
return promisify(fn);
}
// promisify(fn) === promiseFn => true
One problem I didn't think of when opening #41 is that the result of some core methods can change when using
One solution would be for |
Closing this out for now. The trade-offs do not seem worth the changes at this time:
|
Opening to get the conversation started on leveraging native
promisify
when the executing process supports it. Updates use nativepromisify
when available and none of thepify
options imply functionality it cannot support. Performance testing may inform whether these changes are worth while or not.Resolves #41
Todo: