forked from loverajoel/jstips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request loverajoel#420 from bhaskarmelkani/hash-maps-witho…
…ut-side-effects Hash maps with no side effects
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
layout: *post | ||
|
||
title: Hash maps without side effects | ||
tip-number: 73 | ||
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) |