Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Created August 3, 2016 04:59
Show Gist options
  • Save ryanmorr/9bee0cf46dd8c4d587f55caac5c4bb1f to your computer and use it in GitHub Desktop.
Save ryanmorr/9bee0cf46dd8c4d587f55caac5c4bb1f to your computer and use it in GitHub Desktop.
A superior alternative to object literals for basic hash maps
function hashmap(...props) {
const map = Object.create(null, {
[Symbol.iterator]: {
enumerable: false,
writable: false,
configurable: false,
value: () => ({
items: Object.entries(map),
next: function next() {
return {
done: this.items.length === 0,
value: this.items.shift()
};
}
})
}
});
if (props.length) {
Object.assign(map, ...props);
}
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment