Chunk test

Benchmark created on


Setup

const MAX_SIZE_WITH_BUFFER = (4 * 1024 * 1024) * .8; 

function doesMessageExceedMaxSize(items, maxSize) {
  const serialized = JSON.stringify(items);
  return serialized.length > maxSize;
}

function createMockProduct(id, size) {
  const baseProduct = {
    external_id: `gid://shopify/Product/${id}`,
    external_created_at: new Date().toISOString(),
    external_updated_at: new Date().toISOString(),
    title: `Test Product ${id}`,
    description: "A test product",
    vendor: "Test Vendor",
    product_type: "Test Type",
    status: "ACTIVE",
    tags: ["test", "benchmark"],
  };

  if (size === "medium") {
    return {
      ...baseProduct,
      description: baseProduct.description.repeat(50),
      tags: Array.from({ length: 20 }, (_, i) => `tag-${i}`),
    };
  }

  if (size === "large") {
    return {
      ...baseProduct,
      description: baseProduct.description.repeat(200),
      tags: Array.from({ length: 100 }, (_, i) => `tag-${i}`),
    };
  }

  return baseProduct;
}

const testProducts = Array.from({ length: 500 }, (_, i) => {
  const size = i % 3 === 0 ? "large" : i % 3 === 1 ? "medium" : "small";
  return createMockProduct(i, size);
});

Test runner

Ready to run.

Testing in
TestOps/sec
Imperative
function chunkProductsBySize(products) {
  const batches = [];
  let currentBatch = [];

  for (const product of products) {
    currentBatch.push(product);

    if (doesMessageExceedMaxSize(currentBatch, MAX_SIZE_WITH_BUFFER)) {
      currentBatch.pop();
      batches.push(currentBatch);
      currentBatch = [product];
    }
  }

  if (currentBatch.length > 0) {
    batches.push(currentBatch);
  }

  return batches;
}

chunkProductsBySize(testProducts);
ready
reduce
function chunkByMaxSize(items) {
  if (items.length === 0) {
    return [];
  }

  const result = items.reduce(
    (acc, item) => {
      const potentialChunk = [...acc.currentChunk, item];

      if (doesMessageExceedMaxSize(potentialChunk, MAX_SIZE_WITH_BUFFER)) {
        return {
          chunks:
            acc.currentChunk.length > 0
              ? [...acc.chunks, acc.currentChunk]
              : acc.chunks,
          currentChunk: [item],
        };
      }

      return {
        chunks: acc.chunks,
        currentChunk: potentialChunk,
      };
    },
    { chunks: [], currentChunk: [] },
  );

  return result.currentChunk.length > 0
    ? [...result.chunks, result.currentChunk]
    : result.chunks;
}

chunkByMaxSize(testProducts);
ready

Revisions

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