June 24th, 2024

New JavaScript Set Methods

New JavaScript Set methods introduced in major browsers like Firefox 127 offer efficient set operations without polyfills. These methods simplify tasks like finding intersections, unions, and subsets, enhancing working with unique collections.

Read original articleLink Icon
New JavaScript Set Methods

New JavaScript Set methods have been introduced, available in major browser engines since Firefox 127 without the need for polyfills. These methods include intersection(), union(), difference(), symmetricDifference(), isSubsetOf(), isSupersetOf(), and isDisjointFrom(). Sets in JavaScript are unique collections where each value is stored only once, similar to arrays but with guaranteed uniqueness. Sets offer advantages like faster element checks compared to arrays, making them suitable for performance-critical scenarios. The new methods allow for various set operations like finding intersections, unions, differences, and logical comparisons between sets. These operations simplify tasks such as removing duplicates, equality checks, and comparisons. The methods also enable checking for subsets, supersets, and disjoint sets, providing Boolean results for logical checks without returning new sets. Overall, the new JavaScript Set methods enhance the functionality and efficiency of working with unique collections in JavaScript.

Related

The demise of the mildly dynamic website (2022)

The demise of the mildly dynamic website (2022)

The evolution of websites from hand-crafted HTML to PHP enabled dynamic web apps with simple deployment. PHP's decline led to static site generators replacing mildly dynamic sites, shifting to JavaScript for features like comments.

Exposition of Front End Build Systems

Exposition of Front End Build Systems

Frontend build systems are crucial in web development, involving transpilation, bundling, and minification steps. Tools like Babel and Webpack optimize code for performance and developer experience. Various bundlers like Webpack, Rollup, Parcel, esbuild, and Turbopack are compared for features and performance.

Software Engineering Practices (2022)

Software Engineering Practices (2022)

Gergely Orosz sparked a Twitter discussion on software engineering practices. Simon Willison elaborated on key practices in a blog post, emphasizing documentation, test data creation, database migrations, templates, code formatting, environment setup automation, and preview environments. Willison highlights the productivity and quality benefits of investing in these practices and recommends tools like Docker, Gitpod, and Codespaces for implementation.

Asynchronous Consensus Without Trusted Setup or Public-Key Cryptography

Asynchronous Consensus Without Trusted Setup or Public-Key Cryptography

Researchers propose an Asynchronous Common Subset (ACS) protocol for Byzantine consensus without trusted setup or public-key cryptography. The protocol uses hash functions, offers post-quantum security, and introduces new primitives. Efficiently evaluated in a geo-distributed setting.

Gren 0.4: New Foundations

Gren 0.4: New Foundations

Gren 0.4 updates its functional language with enhanced core packages, a new compiler, revamped FileSystem API, improved functions, and a community shift to Discord. These updates aim to boost usability and community engagement.

Link Icon 11 comments
By @esprehn - 5 months
I'm very excited this finally shipped everywhere! Prior to this Set wasn't much more than a Map<any, true> since it had no actual Set operations.
By @mcphage - 5 months
Oh, that's a relief. Having "set" objects in JS, without having any of the standard set operations, didn't make a lot of sense.
By @bsmth - 5 months
Blog post author here in case there's any feedback or questions. Thanks for sharing :)
By @wruza - 5 months
It was replaceAll in 2021 and now this! Who knows what we’ll get in 2030. Maybe Array.prototype.last() even.
By @orangepanda - 5 months
> It's also typically faster to check if an element is in a set as opposed to in an Array

What optimisations allow that? As sets in javascript maintain insertion order, aren’t lookups O(n) ?

By @philipwhiuk - 5 months
It's a pity that you can't implement a function in an object to consider it equal to another object with the same member values.
By @throw156754228 - 5 months
This'll help with AoC. Just need sensible tuples now and we'll be golden. See you in ten years.
By @bavell - 5 months
Perhaps it's asking too much but it would be great to see performance comparisons of these new set methods versus naive JS.

Not sure when I'll have a chance to use them but seems pretty comprehensive in covering all the basics.

By @pipeline_peak - 5 months
[6,-2,2,-7].sort()

Result : [-2,-7,2,6]

Ladies and Gentlemen, the lingua franca of web programming.

By @twosdai - 5 months
Huh. I might actually start using sets over just simple objects. Never found a great reason to use them before these methods, other than to show off.
By @mrkeen - 5 months
Damn, in 2024?

Do Java next!