Instruction count limit of 11M reached on cart transform function

Use case

our usecase is very simple we need to merge 3 -4 items into a single bundle in cart and checkout , which we are able to do using cart transform function but the limitation of instruction count only lets us operate on line items upto 60 max , which only make upto 15 - 20 bundles depending on how many items are in bundle
after that the instruction count limit is reached and function stops working

We have three to four lineitems added to cart per bundle, these items can be different based on the selections made on the PDP page by the customer and when adding to cart we assign a unique bundle ID to these items
then in cart transform we merge those items into one bundle using merge operation

we need to target atleast 100 bundles in a single cart

here is to code for the cart transform function

export function run(input) {
  const bundles = makeBundles(input.cart.lines);
  if(bundles.length) return { operations: bundles }
  return NO_CHANGES
}

function makeBundles(cartLines) {
  const bundles = {};

  for (const cartLine of cartLines) {
    const bundleId = cartLine.bundleId?.value;
    if (!bundleId) continue;

    if (!bundles[bundleId]) {
      const parentProduct = parseParentProduct(cartLine.parentProduct?.value)
      bundles[bundleId] = { 
        merge : {
          cartLines: [], 
          parentVariantId: parentProduct?.defaultVariantId,
          title: parentProduct?.title
        }
      };
      if(cartLine.parentProduct?.imageUrl) bundles[bundleId].merge.image = { url : cartLine.parentProduct?.imageUrl }
    }
    bundles[bundleId].merge.cartLines.push({
      cartLineId:cartLine.id,
      quantity: 1
    });
  }

  return Object.values(bundles);
}
const parseParentProduct = (value) => {
  try {
    return JSON.parse(value.replace(/=>/g, ":"));
  } catch {
    console.error(`Failed to parse parentProduct: ${value}`);
    return null;
  }
}

run.graphql

query RunInput {
  cart {
    lines {
      id
      quantity
      bundleId : attribute(key: "_bundleId"){
        value
      }
      parentProduct : attribute(key: "_parentProduct"){
        value
      }
    }
  }
}

Does anyone knows how can i improve these results , i want to increase the number of lineitems hence number of bundle items in cart

If there is any possibility of further optimization or any other work around to get the desired results
my graphql query is very minimum as well

Any help for improvements is appreciated
Best Regards

1 Like

Are you using the latest Shopify CLI and shopify_function npm package?

Is there any way you can avoid JSON parsing of the cart attribute?

These may help, but to scale to the largest carts, you might need to consider using Rust:

2 Likes

Hey @NickWesselman
Thanks for the advice, With Rust I am able to achieve my desired result as it is consuming very little instruction count as compared JS

1 Like