contains_set

Checks if an array contains a value using optimized Set-based lookup. For large arrays (100+ items), this is significantly faster than the native contains operator, which uses O(n) linear search.

{% assign all_skus = products | map: "sku" %}

{% if all_skus | contains_set: "ABC123" %}
  SKU exists in catalog
{% endif %}

Syntax

{{ array | contains_set: value }}
Parameter
Description

array

Array to search in

value

Value to search for

Performance

Array Size

Native contains

contains_set

100 items

O(100) per check

O(1) per check

10,000 items

O(10,000) per check

O(1) per check

250,000 items

O(250,000) per check

O(1) per check

The filter automatically caches the Set representation, so subsequent lookups on the same array are instant.

Examples

Check if SKU exists in large inventory:

Filter items not in exclusion list:

Validate order items against allowed products:

When to Use

Scenario
Recommended

Single lookup

Use native contains

Multiple lookups on same array

Use contains_set

Array with 100+ items

Use contains_set

Small arrays (<100 items)

Either works, contains_set has slight overhead

Notes

  • Works with strings too: "hello world" | contains_set: "world" returns true

  • Returns false for null or undefined input arrays

Last updated