bluebird还有替代原生 promise的必要吗

测试代码


// global.Promise = require('bluebird').Promise;

console.log('node version:',process.version);
const co = require('co');
function* testfun_co(){
    let total = 0;
    for(let i = 0;i<10000;i++){
        total += yield done=>done(null,i);
    }
    console.log('total:',total);
}

co(function*(){

    console.time('bench origin');
    yield *testfun_co();
    yield *testfun_co();
    console.timeEnd('bench origin');


}).then(ret=>console.log('all test end'));



两个版本的node 测试结果

v8.9.4

//  8.9.4 原生的Promise
➜  bin node "/Users/mac/Documents/testnodejs/pathtest2.js"
node version: v8.9.4
total: 49995000
total: 49995000
bench origin: 23.947ms
all test end
➜  bin node "/Users/mac/Documents/testnodejs/pathtest2.js"
node version: v8.9.4
total: 49995000
total: 49995000
bench origin: 24.263ms
all test end


// 8.9.4 bluebird
➜  bin node "/Users/mac/Documents/testnodejs/pathtest2.js"
node version: v8.9.4
total: 49995000
total: 49995000
bench origin: 43.253ms
all test end
➜  bin node "/Users/mac/Documents/testnodejs/pathtest2.js"
node version: v8.9.4
total: 49995000
total: 49995000
bench origin: 42.724ms
all test end


v6.12.3
// 原生 Promise
➜  bin ./node /Users/mac/Documents/testnodejs/pathtest2.js
node version: v6.12.3
total: 49995000
total: 49995000
bench origin: 56.907ms
all test end
➜  bin ./node /Users/mac/Documents/testnodejs/pathtest2.js
node version: v6.12.3
total: 49995000
total: 49995000
bench origin: 53.840ms
all test end


// blubird
➜  bin ./node /Users/mac/Documents/testnodejs/pathtest2.js
node version: v6.12.3
total: 49995000
total: 49995000
bench origin: 39.129ms
all test end
➜  bin ./node /Users/mac/Documents/testnodejs/pathtest2.js
node version: v6.12.3
total: 49995000
total: 49995000
bench origin: 38.773ms
all test end

  • 对于8.x版本的node,可能只有 promisify 还有用?
  • 不过 node8.0加入了一个新API util.promisify
  • 还在使用老版本node的同学可以继续用bluebird以提升性能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值