Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Last active August 5, 2022 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinocarl/3cc15c9a8dea615c85791ee785b5bae8 to your computer and use it in GitHub Desktop.
Save dinocarl/3cc15c9a8dea615c85791ee785b5bae8 to your computer and use it in GitHub Desktop.
Different ways of checking whether all items in an array are equal
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