Promise.all()比对每个诺言使用await慢吗?

时间:2019-04-05 04:53:59

标签: javascript node.js asynchronous promise es6-promise

我正在做一些性能测试,并且正在查看node / js中的Promise.all()。但是,测试之后,它实际上比使用await来解决我要解决的3个承诺要慢。我只是从mongodb数据库返回一些数据。

我在这里做错什么了吗,或者这仅仅是由于事件循环的工作方式引起的吗?

Promise.all():234.820ms

  // Init db connection
  const db = client.db('docker-client1');
  const productsCollection = db.collection('ICProductData');
  const rulesCollection = db.collection('SPRuleData');
  const customersCollection = db.collection('ARCustomerData');
  // Main function

  const CustomerCode = 'FINE';
  const ProductCode = 'BEDCABINET';

  let customers = customersCollection
    .find({ _id: CustomerCode })
    .project({ PriceCode: 1, BillToAccountCode: 1 })
    .toArray();

  let products = productsCollection
    .find({ _id: ProductCode })
    .project({ 'Price.PriceCode': 1, 'Price.SellingPrice': 1 })
    .toArray();

  let rules = rulesCollection
    .find({
      $and: [
        {
          $or: [
            {
              $and: [
                { What1Code: ProductCode, What1Type: 'Product' },
                { Who1Code: CustomerCode, Who1Type: 'Customer' }
              ]
            }
          ]
        }
      ]
    })
    .toArray();

  const results = await Promise.all([customers, products, rules]);
  console.timeEnd();

仅使用await:127.239ms

  // Init db connection
  const db = client.db('docker-client1');
  const productsCollection = db.collection('ICProductData');
  const rulesCollection = db.collection('SPRuleData');
  const customersCollection = db.collection('ARCustomerData');
  // Main function

  const CustomerCode = 'FINE';
  const ProductCode = 'BEDCABINET';

  const custReq = await customersCollection
    .find({ _id: CustomerCode })
    .project({ PriceCode: 1, BillToAccountCode: 1 })
    .toArray();
  const prodReq = await productsCollection
    .find({ _id: ProductCode })
    .project({ 'Price.PriceCode': 1, 'Price.SellingPrice': 1 })
    .toArray();

  let rulesReq = await rulesCollection
    .find({
      $and: [
        {
          $or: [
            {
              $and: [
                { What1Code: ProductCode, What1Type: 'Product' },
                { Who1Code: CustomerCode, Who1Type: 'Customer' }
              ]
            }
          ]
        }
      ]
    })
    .toArray();

  console.timeEnd();

0 个答案:

没有答案