Repository: parcel-bundler/parcel
File: packages/bundlers/default/src/DefaultBundler.js (JavaScript)

Code before:
            // Stop iterating through bundleToRemove's sourceBundles as the bundle has been removed.
            break;
          } else {
            bundleGraph.removeEdge(sourceBundleId, bundleIdToRemove);
          }
        }
        numBundlesInGroup--;
      }
    }
  }

  // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
  if (modifiedSourceBundles.size > 0) {
    let assetOrderMap = new Map(assets.map(a => [a, assets.indexOf(a)]));

    for (let bundle of modifiedSourceBundles) {
      bundle.assets = new Set(
        [...bundle.assets].sort((a, b) => {
          let aIndex = nullthrows(assetOrderMap.get(a));
          let bIndex = nullthrows(assetOrderMap.get(b));

          return aIndex - bIndex;
        }),
      );
    }
  }

  function deleteBundle(bundleRoot: BundleRoot) {
    bundleGraph.removeNode(nullthrows(bundles.get(bundleRoot.id)));
    bundleRoots.delete(bundleRoot);
    bundles.delete(bundleRoot.id);
    if (bundleRootGraph.hasContentKey(bundleRoot.id)) {
      bundleRootGraph.removeNode(
        bundleRootGraph.getNodeIdByContentKey(bundleRoot.id),
      );
    }
  }
  function getBundleGroupsForBundle(nodeId: NodeId) {
    let bundleGroupBundleIds = new Set();
    bundleGraph.traverseAncestors(nodeId, ancestorId => {
      if (
        bundleGraph
          .getNodeIdsConnectedTo(ancestorId) //if node is root, then dont add, otherwise do add.
          .includes(bundleGraph.rootNodeId)
      ) {
        bundleGroupBundleIds.add(ancestorId);
      }
    });
    return bundleGroupBundleIds;
  }
  function getBundlesForBundleGroup(bundleGroupId) {

Review comment (suggestion):
Wonder if you could use BitSet for `bundle.assets` here? Then when you do `bundle.assets.add` it would just be setting a bit corresponding to the position within `assets`. When we take the ideal graph and convert it back to the legacy graph we could iterate in asset order (as `BitSet#values()` does).

Code after:
            // Stop iterating through bundleToRemove's sourceBundles as the bundle has been removed.
            break;
          } else {
            bundleGraph.removeEdge(sourceBundleId, bundleIdToRemove);
          }
        }
        numBundlesInGroup--;
      }
    }
  }

  // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
  if (modifiedSourceBundles.size > 0) {
    let assetOrderMap = new Map(assets.map((a, index) => [a, index]));

    for (let bundle of modifiedSourceBundles) {
      bundle.assets = new Set(
        [...bundle.assets].sort((a, b) => {
          let aIndex = nullthrows(assetOrderMap.get(a));
          let bIndex = nullthrows(assetOrderMap.get(b));

          return aIndex - bIndex;
        }),
      );
    }
  }

  function deleteBundle(bundleRoot: BundleRoot) {
    bundleGraph.removeNode(nullthrows(bundles.get(bundleRoot.id)));
    bundleRoots.delete(bundleRoot);
    bundles.delete(bundleRoot.id);
    if (bundleRootGraph.hasContentKey(bundleRoot.id)) {
      bundleRootGraph.removeNode(
        bundleRootGraph.getNodeIdByContentKey(bundleRoot.id),
      );
    }
  }
  function getBundleGroupsForBundle(nodeId: NodeId) {
    let bundleGroupBundleIds = new Set();
    bundleGraph.traverseAncestors(nodeId, ancestorId => {
      if (
        bundleGraph
          .getNodeIdsConnectedTo(ancestorId) //if node is root, then dont add, otherwise do add.
          .includes(bundleGraph.rootNodeId)
      ) {
        bundleGroupBundleIds.add(ancestorId);
      }
    });
    return bundleGroupBundleIds;
  }
  function getBundlesForBundleGroup(bundleGroupId) {