-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (53 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict'
var caches = new WeakMap()
function StringCacheMap() {
var limit = arguments[0];
var hard = arguments[1];
if (!this || this.constructor !== StringCacheMap)
throw new TypeError("Constructor StringCacheMap requires 'new'")
caches.set(this, {
limit: limit || 16,
hard: hard !== false,
victims: Object.create(null),
entries: Object.create(null),
count: 0,
})
}
StringCacheMap.prototype.delete = function(key) {
if (!this.has(key)) return false
var cache = caches.get(this)
var entries = cache.entries
var victims = cache.victims
delete entries[key]
delete victims[key]
return true
}
StringCacheMap.prototype.get = function(key) {
var cache = caches.get(this)
var entries = cache.entries
var victims = cache.victims
var hard = cache.hard
if (key in entries) return entries[key]
if (key in victims)
if (hard) this.set(key, victims[key])
else entries[key] = victims[key]
return entries[key]
}
StringCacheMap.prototype.has = function(key) {
var cache = caches.get(this)
var entries = cache.entries
var victims = cache.victims
return key in entries || key in victims
}
StringCacheMap.prototype.set = function(key, value) {
var cache = caches.get(this)
cache.count += 1
if (cache.count > cache.limit) {
cache.count = 1
cache.victims = cache.entries
cache.entries = Object.create(null)
}
cache.entries[key] = value
return this
}
module.exports = StringCacheMap