Promise.all vs no Promise.all on array of awaited promises (v5)

Revision 5 of this benchmark created on


Setup

const rawParams = {
  country_code: 'US',
  city: 'New York',
  postal_code: '10001',
  region_code: 'NY',
  other_key: 'Some Value',
  null_key: null
};

function hashString({ rawString, algorithm }) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(`${algorithm}_${rawString}`), Math.random() * 100);
  });
}

async function processParamsWithPromiseAll(params) {
  return Promise.all(
    Object.entries(params).map(async ([key, value]) => {
      if (key === 'country_code' && typeof value === 'string') {
        return ['country', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
      }
      if (key === 'city' && typeof value === 'string') {
        return ['ct', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
      }
      if (key === 'postal_code' && typeof value === 'string') {
        return ['zp', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
      }
      if (key === 'region_code' && typeof value === 'string') {
        return ['st', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
      }
      if (value != null) return [key, value];
      return [key, null];
    })
  );
}

async function processParamsWithPromiseAllAndArrayOfPromises(params) {
  return Promise.all(
    Object.entries(rawParams).map(([key, value]) => {
      let transformedKey = key;
      let valuePromise;

      if (key === "country_code" && typeof value === "string") {
        transformedKey = "country";
        valuePromise = hashString({
          rawString: value.trim().toLowerCase(),
          algorithm: "sha256",
        });
      } else if (key === "city" && typeof value === "string") {
        transformedKey = "ct";
        valuePromise = hashString({
          rawString: value.trim().toLowerCase().replace(/\s+/g, ""),
          algorithm: "sha256",
        });
      } else if (key === "postal_code" && typeof value === "string") {
        transformedKey = "zp";
        valuePromise = hashString({
          rawString: value.trim().toLowerCase(),
          algorithm: "sha256",
        });
      } else if (key === "region_code" && typeof value === "string") {
        transformedKey = "st";
        valuePromise = hashString({
          rawString: value.trim().toLowerCase().replace(/\s+/g, ""),
          algorithm: "sha256",
        });
      } else {
        valuePromise = Promise.resolve(value);
      }

      return valuePromise.then((resolvedValue) => ({
        [transformedKey]: resolvedValue,
      }));
    })
  );
}

async function processParamsWithPromiseAllnoAwait(params) {
  return Promise.all(
    Object.entries(params).map(([key, value]) => {
      if (key === 'country_code' && typeof value === 'string') {
        return ['country', hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
      }
      if (key === 'city' && typeof value === 'string') {
        return ['ct', hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
      }
      if (key === 'postal_code' && typeof value === 'string') {
        return ['zp', hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
      }
      if (key === 'region_code' && typeof value === 'string') {
        return ['st', hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
      }
      if (value != null) return [key, value];
      return [key, null];
    })
  );
}


async function processParamsSequential(params) {
  const out = [];
  for (const [key, value] of Object.entries(params)) {
    if (key === 'country_code' && typeof value === 'string') {
      out.push(['country', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })]);
    } else if (key === 'city' && typeof value === 'string') {
      out.push(['ct', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })]);
    } else if (key === 'postal_code' && typeof value === 'string') {
      out.push(['zp', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })]);
    } else if (key === 'region_code' && typeof value === 'string') {
      out.push(['st', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })]);
    } else if (value != null) {
      out.push([key, value]);
    } else {
      out.push([key, null]);
    }
  }
  return out;
}

// Create an array of promises that handle all transformations
    const transformationPromises = Object.entries(rawParams).map(([key, value]) => {
      let transformedKey = key;
      let valuePromise;

      if (key === 'country_code' && typeof value === 'string') {
        transformedKey = 'country';
        valuePromise = hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' });
      } else if (key === 'city' && typeof value === 'string') {
        transformedKey = 'ct';
        valuePromise = hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' });
      } else if (key === 'postal_code' && typeof value === 'string') {
        transformedKey = 'zp';
        valuePromise = hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' });
      } else if (key === 'region_code' && typeof value === 'string') {
        transformedKey = 'st';
        valuePromise = hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' });
      } else {
        // For other cases, resolve immediately without waiting for a promise
        valuePromise = Promise.resolve(value);
      }

      // Return the promise along with its transformed key
      return valuePromise.then((resolvedValue) => ({ [transformedKey]: resolvedValue }));
    });
 async function processBatchedPromises(params) {Promise.all(params);}   
    

Test runner

Ready to run.

Testing in
TestOps/sec
Promise.all with await on hashString
processParamsWithPromiseAll(rawParams).then(() => deferred.resolve());

ready
Promise.all no await on hashString
processParamsWithPromiseAllnoAwait(rawParams).then(() => deferred.resolve());
ready
no Promise.all just await on hashString
processParamsSequential(rawParams).then(() => deferred.resolve());
ready
Promise.all array of promises
processParamsWithPromiseAllAndArrayOfPromises(rawParams).then(() => deferred.resolve());
ready
Promise.all batch promises
processBatchedPromises(transformationPromises).then(() => deferred.resolve());
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.