├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | - '4' 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | class StatsMap extends Map { 3 | 4 | constructor(iterable) { 5 | super(iterable); 6 | 7 | this._stats = { 8 | hits: 0, 9 | misses: 0 10 | }; 11 | } 12 | 13 | get stats() { 14 | return this._stats; 15 | } 16 | 17 | set(key, value) { 18 | const data = value.data; 19 | 20 | Object.defineProperty(value, 'data', { 21 | get: () => { 22 | this._stats.misses--; 23 | this._stats.hits++; 24 | return data; 25 | } 26 | }); 27 | 28 | super.set(key, value); 29 | } 30 | 31 | get(key) { 32 | this._stats.misses++; 33 | return super.get(key); 34 | } 35 | 36 | has(key) { 37 | const has = super.has(key); 38 | 39 | if (!has) { 40 | this._stats.misses++; 41 | } 42 | 43 | return has; 44 | } 45 | } 46 | 47 | module.exports = StatsMap; 48 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Sam Verschueren (github.com/SamVerschueren) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stats-map", 3 | "version": "1.0.0", 4 | "description": "Map that keeps track of the hits and misses", 5 | "license": "MIT", 6 | "repository": "SamVerschueren/stats-map", 7 | "author": { 8 | "name": "Sam Verschueren", 9 | "email": "sam.verschueren@gmail.com", 10 | "url": "github.com/SamVerschueren" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "clinton && xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "map", 23 | "stats", 24 | "statistics", 25 | "hits", 26 | "misses", 27 | "iterable" 28 | ], 29 | "devDependencies": { 30 | "ava": "*", 31 | "clinton": "*", 32 | "xo": "*" 33 | }, 34 | "xo": { 35 | "esnext": true 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # stats-map [![Build Status](https://travis-ci.org/SamVerschueren/stats-map.svg?branch=master)](https://travis-ci.org/SamVerschueren/stats-map) 2 | 3 | > [Mem](https://github.com/sindresorhus/mem) cache [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) that keeps track of the hits and misses 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save stats-map 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const StatsMap = require('./'); 17 | const mem = require('mem'); 18 | 19 | let i = 0; 20 | const counter = () => ++i; 21 | 22 | const cache = new StatsMap(); 23 | const memoized = mem(counter, {cache}); 24 | 25 | memoized('foo'); 26 | //=> 1 27 | 28 | // cached as it's the same argument 29 | memoized('foo'); 30 | //=> 1 31 | 32 | // not cached anymore as the argument changed 33 | memoized('bar'); 34 | //=> 2 35 | 36 | memoized('bar'); 37 | //=> 2 38 | 39 | console.log(cache.stats); 40 | //=> {hits: 2, misses: 2} 41 | ``` 42 | 43 | 44 | ## API 45 | 46 | ### StatsMap([iterable]) 47 | 48 | Inherits from [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). 49 | 50 | ##### .stats 51 | 52 | Type: `object` 53 | 54 | The statistics of the map like the `hits` and `misses`. 55 | 56 | 57 | ## Related 58 | 59 | - [mem](https://github.com/sindresorhus/mem) - Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input. 60 | 61 | 62 | ## License 63 | 64 | MIT © [Sam Verschueren](https://github.com/SamVerschueren) 65 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import StatsMap from './'; 3 | 4 | test('initial stats', t => { 5 | const map = new StatsMap(); 6 | 7 | t.deepEqual(map.stats, { 8 | hits: 0, 9 | misses: 0 10 | }); 11 | }); 12 | 13 | test('stats', t => { 14 | const map = new StatsMap(); 15 | map.set('foo', {data: 'bar'}); 16 | 17 | const ret = map.get('foo'); 18 | 19 | t.deepEqual(map.stats, { 20 | hits: 0, 21 | misses: 1 22 | }); 23 | 24 | t.deepEqual(ret.data, 'bar'); 25 | 26 | t.deepEqual(map.stats, { 27 | hits: 1, 28 | misses: 0 29 | }); 30 | 31 | map.has('foo'); 32 | map.has('unicorn'); 33 | 34 | t.deepEqual(map.stats, { 35 | hits: 1, 36 | misses: 1 37 | }); 38 | }); 39 | --------------------------------------------------------------------------------