logo
post image

Firefox 127 Released, Adds New Methods for Comparison of Sets

Firefox 127 was released on 11th June 2024. It has added many updates, however the best part is that it has implemented multiple Set methods for comparison of two Sets. This can be quite useful to carry out day-to-day common operations. With Firefox implementing this, these have now achieved cross browser compatabilty.

Following methods have been added to the Set object, they all perform operations on 2 Sets :

  • intersection() : Returns a new Set holding common elements in the 2 Sets
  • union() : Returns a new Set holding elements of both Sets
  • difference() : Returns a new Set holding elements not present in the second Set
  • symmetricDifference() : Returns a new Set holding uncommon elements in 2 Sets
  • isSubsetOf() : Returns a boolean indicating whether all elements of one Set are contained in the second Set
  • isSupersetOf() : Works like isSubsetOf(), however order of Sets are reversed
  • isDisjointFrom() : Returns a boolean indicating whether two Sets have no common elements

These methods will make things easier for developers. For eg, previously if we were to find common elements in 2 Javascript arrays, the code would look like the below.

const arrayFirst = [1, 2, 3, 4]
const arraySecond = [3, 4, 5, 6]

const arrayIntersection = arrayFirst.filter(function(x) {
	// checking second array contains the element "x"
	if(arraySecond.indexOf(x) !== -1)
		return true
	else
		return false
})

// [3, 4]
console.log(arrayIntersection)

With the new intersection() method, the code would become a bit more structured.

const arrayFirst = new Set([1, 2, 3, 4])
const arraySecond = new Set([3, 4, 5, 6])

const arrayIntersection = [...arrayFirst.intersection(arraySecond)]

// [3, 4]
console.log(arrayIntersection)

Checkout the full list of features implemented in Firefox 127.