Skip to content

Commit

Permalink
Hash maps with no side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaskarmelkani committed Aug 16, 2017
1 parent 7ea9f69 commit 381e776
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions _posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 381e776

Please sign in to comment.