Generic Comparison ?
- Stage: Unstaged
- Status: Withdrawn
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal addresses the lack of a standard way to compare arrays in JavaScript, including nested structures. It proposes an Array.prototype.compare method that takes a comparator function and returns -1, 0, or 1, and also explores a <=> three-way comparison operator. The proposal is at an early stage, with no specification details or maturity further than an idea.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Generic Comparison
Author: Hemanth HM
Champions: Jordan Harband & Hemanth HM
Motivation
Comparing arrays are one of the most common operations we do on arrays, and there is no consitent standard way to compare two array and given that array might have any type of primitives and nested structures, it get more tedious to compare them.
How is this currently handled?
There is no standard way to do it, there are few modules like array-equal, deep-equal with like 5M and 8M downloads per week respectivly, which is subset of deep comparison.
Consider few examples:
node builtin:
How we could do it?
Array.prototype.compare takes a comparator function, which gets two arguments.
Each a tuple of [value, key] and it returns < 0, 0, or > 1 (and throws when a non-finite-non-integer is returned) and then it would return either -1, 0, or 1
That way we could write our own logic, and get back 0 if they were equal.
P.S: This was a proposal evolved Array.prototype.equals.
Extending the idea to a three-way comparison:
We could rather have a <=> three-way compare operator, that does the below: