很多天前old big让我测试在async中throw和return的效率,偶然踩中了坑
async function foo() {
throw new Error();
}
(async function test() {
for (let i = 0; i < 100000; i++) {
try {
await foo();
} catch (e) {
}
}
})();
console.log('done');
是什么原因呢?
下面仅仅四行代码也硬了
for (let i = 0; i < 100000; i++) {
Promise.reject().catch(e => { });
}
console.log('done');
// 只需一行代码。node就冷静下来了
global.Promise = require('bluebird');
for (let i = 0; i < 100000; i++) {
Promise.reject().catch(e => { });
}
console.log('done');