diff --git a/src/mol-data/int/impl/sorted-array.ts b/src/mol-data/int/impl/sorted-array.ts index 1a27740edc21bc645397aaa797c06ab170e0a6ae..9aed1302597de299afe4d3cd3c83976e1429d9c7 100644 --- a/src/mol-data/int/impl/sorted-array.ts +++ b/src/mol-data/int/impl/sorted-array.ts @@ -171,13 +171,17 @@ export function isSubset(a: Nums, b: Nums) { return equal === lenB; } -export function union(a: Nums, b: Nums) { +export function union(a: Nums, b: Nums): Nums { if (a === b) return a; + const lenA = a.length, lenB = b.length; + if (lenA === 0) return b; + if (lenB === 0) return a; + if (a[0] > b[0]) return union(b, a); + const { startI, startJ, endI, endJ } = getSuitableIntersectionRange(a, b); const commonCount = getCommonCount(a, b, startI, startJ, endI, endJ); - const lenA = a.length, lenB = b.length; // A === B || B is subset of A ==> A if ((commonCount === lenA && commonCount === lenB) || commonCount === lenB) return a; // A is subset of B ===> B