简体   繁体   English

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

[英]Promise.all() is slower than using await for each promise?

I'm doing some testing for performance and was looking at Promise.all() in node.js. However, after testing this it's actually slower than using await for the 3 promises I want to resolve.我正在做一些性能测试,正在查看 node.js 中的 Promise.all()。但是,在测试之后,它实际上比使用 await 来处理我想要解决的 3 个承诺要慢。 I'm simply returning some data back from a mongodb database.我只是从 mongodb 数据库返回一些数据。

Am I doing something wrong here or is this just due to the way the event loop is working?我在这里做错了什么还是仅仅是由于事件循环的工作方式?

Promise.all(): 234.820ms Promise.all():234.820 毫秒

  // 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();

Simply using await: 127.239ms简单地使用 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();

The method toArray() does not return a Promise in Old versions of Mongo. toArray() 方法在旧版本的 Mongo 中不返回 Promise。 This could be the explanation about timings.这可能是关于时间的解释。 You could put a console.log with timing after each toArray() they should run in the same millisecond (same tick).您可以在每个 toArray() 之后放置一个带有计时的 console.log,它们应该在相同的毫秒(相同的刻度)内运行。 If this is not the case, it could be an indication that toArray() is waiting for the response... so you are running sequentially.如果不是这种情况,则可能表明 toArray() 正在等待响应......因此您正在按顺序运行。 You could try to resolve this with: let myProm = collection.find(filter).exec()您可以尝试通过以下方式解决此问题:let myProm = collection.find(filter).exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM