This is an attempt at getting a promise library to run as fast as native callbacks while still offering Promises/A+ compatability. It was sparked by these tweets:
We would not give up perf in core. You can already do this in userland if you have diff priorities.
It would take some work to optimise and you couldn’t use something like Q, but I’d be surprised if it couldn’t be done.
node promises will have to be as fast as "Base" (current callbacks) in this benchmark
It's intended as a proof of concept, The .then
method isn't quite good enough to pass the Promises/A+ test suite and crucially it doesn't have any way of handling non-truthy errors. I also haven't spent any time testing it, but it is principally possible to create a then
method that is compliant except for the flasy errors issue on top of this implimentaiton.
Note that I've not actually used the .then
method, instead I've created a much more optimised .nodeify
method. Anyone who uses promises seriously as promises will of course get the slight performance drop of using a real .then
method (probably a perf drop of about 30% - 40%). The point I wanted to make though is that you can make the .then
method available while still providing a high performance alternative for those who don't have complex control flow problems to manage.
I agree, work almost certainly needs to be done by hacking node core (perhaps even a C++ implementation?). Unfortunately my knowledge of C++ is extremely limited and my desire to fork node.js and then maintain my own fork is even lower.
I actually think the aim should be to ensure that
lstat(path, cb)
in the version that supports promises is as fast aslstat(path, cb)
in a version that does not support promises but that you can still dolstat(path).then(...
and get a real promise.We need to ensure that it is no slower for callback users, not that promise users see no slow down.
For example:
satisfies the requirements of returning a promise when no callback is passed in, and is an almost perfect match for performance when compared with the pure callback version. It's a bit of an ugly hack, but there you have it, it does work.