Set methods in JavaScript: union, intersection, difference
Comparing two lists of IDs is everyday work in admin scripts: which posts are selected but not yet saved, which tags two posts share, which users were added since the last sync. Until recently, every project wrote the same helpers with filter() and includes(). ES2025 added the set operations to Set itself: union(), intersection(), difference(), symmetricDifference(), and the comparisons isSubsetOf(), isSupersetOf() and isDisjointFrom().
Problem: A bulk-edit screen in a plugin compares the selected post IDs with the IDs already assigned to a collection. The code uses array.filter( ( id ) => ! other.includes( id ) ) in several places; with 5,000 IDs each comparison is slow, and one of the helpers had a bug with string and number IDs.
Solution: Keep the IDs in Set objects of numbers, and use difference() for “to add” and “to remove”, intersection() for shared items, and isSubsetOf() for checks. The methods return new sets and never change the originals.
The operations
const selected = new Set( [ 12, 15, 18, 21 ] );
const assigned = new Set( [ 15, 21, 30 ] );
selected.union( assigned ); // Set { 12, 15, 18, 21, 30 }
selected.intersection( assigned ); // Set { 15, 21 }
selected.difference( assigned ); // Set { 12, 18 } selected, not yet assigned
assigned.difference( selected ); // Set { 30 } assigned, no longer selected
selected.symmetricDifference( assigned ); // Set { 12, 18, 30 }
new Set( [ 15, 21 ] ).isSubsetOf( selected ); // true
selected.isDisjointFrom( new Set( [ 99 ] ) ); // true
All of them take any “set-like” object as the argument: something with size, has() and keys(), which includes a Map. They are implemented with hash lookups, so comparing two sets of 5,000 items takes a few thousand operations instead of the 25 million of a nested includes().
A sync of a collection
async function haSyncCollection( collectionId, selectedIds ) {
const response = await fetch( `/wp-json/ha/v1/collections/${ collectionId }`, {
headers: { 'X-WP-Nonce': window.haBulk.nonce },
} );
const current = new Set( ( await response.json() ).post_ids.map( Number ) );
const wanted = new Set( selectedIds.map( Number ) );
const add = [ ...wanted.difference( current ) ];
const remove = [ ...current.difference( wanted ) ];
if ( ! add.length && ! remove.length ) return;
await fetch( `/wp-json/ha/v1/collections/${ collectionId }`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': window.haBulk.nonce },
body: JSON.stringify( { add, remove } ),
} );
}
The request sends only the changes. map( Number ) on both sides matters: a set compares with SameValueZero, so the string '15' from a data attribute and the number 15 from JSON are different elements. That was the bug in the old helper.
Shared tags of two posts
const tagsA = new Set( postA.tags ); const tagsB = new Set( postB.tags ); const shared = tagsA.intersection( tagsB ); const similarity = shared.size / tagsA.union( tagsB ).size; // Jaccard index, 0..1
A small “related posts” score in the browser, for example in an editor sidebar that suggests links.
Order and copies
The resulting sets keep insertion order, mostly the order of the set the method is called on. Spread ([ ...set ]) or Array.from() turn a result back into an array for JSON. Like the ES2023 array methods, these methods do not mutate: toSorted, toReversed and with.
NOTE: The Set methods are available in Chrome and Edge 122 (February 2024), Safari 17 (September 2023) and Firefox 127 (June 2024), and in Node.js 22. For older browsers, core-js has a polyfill; @wordpress/scripts does not add it automatically.