Data validation (like key uniqueness) may be a separate operation provided by user.
It can be validated even inside key selector callback, but in this case it's better to provide access to the object or map which are filling during associateBy process. Maybe as thisArg (but arrow functions won't work with thisArg) or as a separate argument. Otherwise user needs to create other data structure to store added keys.
const addedKeys = new Set();
Map.associateBy(users, user => {
const key = user.id;
if (addedKeys.has(user.id)) {
throw Error('Duplicate key found');
}
addedKeys.add(key);
return key;
});
You're right, with new .map method for iterables it is working, but you still need to create intermediate tuples, and it's still slower even than polyfill for associateBy (slower by ~68% on jsbench)