UNPKG

859 BJavaScriptView Raw
1'use strict';
2
3function makeCache() {
4 // object with no prototype
5 var cache = Object.create(null);
6
7 // force the jit to immediately realize this object is a dictionary. This
8 // should prevent the JIT from going wastefully one direction (fast mode)
9 // then going another (dict mode) after
10 cache['_cache'] = 1;
11 delete cache['_cache'];
12
13 return cache;
14}
15
16module.exports = Cache;
17function Cache() {
18 this._store = makeCache();
19}
20
21Cache.prototype.set = function(key, value) {
22 return this._store[key] = value;
23};
24
25Cache.prototype.get = function(key) {
26 return this._store[key];
27};
28
29Cache.prototype.has = function(key) {
30 return key in this._store;
31};
32
33Cache.prototype.delete = function(key) {
34 delete this._store[key];
35};
36
37Object.defineProperty(Cache.prototype, 'size', {
38 get: function() {
39 return Object.keys(this._store).length;
40 }
41});