Skip to content

Commit

Permalink
Use Map instead of Object
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 3, 2016
1 parent db24f32 commit f6df3e8
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions workflow/memoization.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

Just calculate the value of something once, and reuse the value avoiding the cost of recalculate the same value.

If you need to control of a set of tiny values, you can use an object:
If you need to control of a set of tiny values, you can use an `Map`:

```js
var cache = {}
var cache = new Map()

var cityOne = 'Murcia'
var cityTwo = 'Madrid'
var routeName = cityOne + '-' + cityTwo
var routeName = `${cityOne}${cityTwo}`

if (!cache[routeName]) cache[routeName] = getDistance(cityOne, cityTwo)
return cache[routeName]
if (!cache.has(routeName)) cache.set(routeName, getDistance(cityOne, cityTwo))
```

If you actually don't know how your cache could grow in memory, be careful. You need a little more sophisticated way as [lru-cache](https://www.npmjs.com/package/lru-cache) to control it.
A little complex but better solution will be control the size of the `Map`, as LRU cache.

0 comments on commit f6df3e8

Please sign in to comment.