Last active
August 5, 2022 02:11
-
-
Save dinocarl/3cc15c9a8dea615c85791ee785b5bae8 to your computer and use it in GitHub Desktop.
Different ways of checking whether all items in an array are equal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const eq = (a) => (b) => a === b; | |
const everyItemEquala = compose( | |
apply(all), | |
juxt([compose(equals, head), identity]) | |
); | |
const everyItemEqualb = compose( | |
equals(1), | |
length, | |
uniq | |
); | |
const everyItemEqualc = (list) => new Set(list).size === 1; | |
const everyItemEquald = compose( | |
equals(1), | |
prop('size'), | |
constructN(1, Set) | |
); | |
const everyItemEquale = (list) => list.every(eq(list[0])); | |
const everyItemEqualf = (list) => { | |
var listLen = list.length - 1; | |
for (var i = listLen; i >= 0; i--) { | |
if (list[i] !== list[0]) return false; | |
} | |
return true; | |
}; | |
// benchmarking https://jsbench.me/0ql6fdslkv/1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment