diff --git a/_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects b/_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects new file mode 100644 index 00000000..0f10ecb5 --- /dev/null +++ b/_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects @@ -0,0 +1,49 @@ +--- +layout: *post + +title: Hash maps without side effects +tip-number: xx +tip-username: bhaskarmelkani +tip-username-profile: https://www.twitter.com/bhaskarmelkani +tip-tldr: Create hash maps(without side effects) using `Object.create(null)`. + +categories: + - en + - javascript +--- +When you want to use javascript object as a hash map(purely for storing data), you might want to create it as follows. + +```js + const map = Object.create(null); +``` + +When creating a map using object literal(`const map = {}`), the map inherites properties from Object by default. It is equivalent to `Object.create(Object.prototype)`. + +But by doing `Object.create(null)`, we explicitly specify `null` as its prototype. So it have absolutely no properties, not even constructor, toString, hasOwnProperty, etc. so you're free to use those keys in your data structure if you need to. + +**Rationale:-** +```js +const dirtyMap = {}; +const cleanMap = Object.create(null); + +dirtyMap.constructor // function Object() { [native code] } + +cleanMap.constructor // undefined + +//iterating maps + +const key; +for(key in dirtyMap){ + if (dirtyMap.hasOwnProperty(key)) { // Check to avoid iterating over inherited properties. + console.log(key + " -> " + dirtyMap[key]); + } +} + +for(key in cleanMap){ + console.log(key + " -> " + cleanMap[key]); // No need to add extra checks, as the object will always be clean +} +``` + +**Note**: +* Object.create() was introduced in ES5 [[Compatibility]](http://kangax.github.io/compat-table/es5/) +* ES6 introduced some new structures: [Map](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map), [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap), [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and [Weak Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet)