Im currently in the process of converting some of my project's dependencies' to their own async / await functions for better development flow. Many of these dependencies such as bcrypt have options to use synchronous ( no callback ) functions or asynchronous ( callback ) functions. I have made a super small function that would convert the output of a bcrypt compare to my framework-friendly version that always returns a {success: true/false, result} object. Heres an example of what I have.
async hashStringCompare (hashed, string) {
try {
const success = bcrypt.compareSync(string, hashed)
if (!success) return {msg: 'The string does not match', success}
return {msg: 'The string does match', success}
} catch (err) {
return {err, msg: 'Something bad happened', success: false}
}
},
The problem is, bcrypt offers two functions for comparing strings and hashed strings; compareSync and compare. If i formatted the code to use the compare function that uses callbacks, it would look like this:
async hashStringCompare (hashed, string) {
return new Promise((resolve) => {
bcrypt.compare(string, hashed, (err, success) => {
if (err) resolve({ err, msg: 'An error has occured.', success })
resolve({success, msg: 'The string does match' })
})
})
}
Really my goal is to get every function with a callback converted to a promise-based format, but my question is this; Which one of the above do you think would be overall more performance friendly? Technically. both functions are asynchronous, but which one do you believe is faster?
If the way i'm structuring this code seems illogical, please tell me. I'm not sure if this is the best way to format non-breaking code, so I would love any feedback regarding this.
Thanks for taking the time to read this. I would really appreciate any advice you may have.