Permalink
Browse files

minor style fixups for DRYness

  • Loading branch information...
1 parent 806a067 commit c62cc2eb04445637f7b6454e5a21a22ca0199250 @isaacs committed Nov 21, 2013
Showing with 19 additions and 31 deletions.
  1. +19 −31 lib/lru-cache.js
View
@@ -14,41 +14,28 @@ function hOP (obj, key) {
function naiveLength () { return 1 }
function LRUCache (options) {
- if (!(this instanceof LRUCache)) {
+ if (!(this instanceof LRUCache))
@flockonus

flockonus Nov 29, 2013

Sorry the noobness, what is this pattern about?

@isaacs

isaacs Nov 29, 2013

Owner

It ensures that the function is only called as a constructor. It the user doesn't use new, it'll still return a proper instance of the class.

return new LRUCache(options)
- }
- if (typeof options === 'number') {
+ if (typeof options === 'number')
options = { max: options }
- }
- if (!options) options = {}
+ if (!options)
+ options = {}
this._max = options.max
+ // Kind of weird to have a default max of Infinity, but oh well.
+ if (!this._max || !(typeof this._max === "number") || this._max <= 0 )
+ this._max = Infinity
this._lengthCalculator = options.length || naiveLength
-
- if (typeof this._lengthCalculator !== "function") {
+ if (typeof this._lengthCalculator !== "function")
@tkissing

tkissing Dec 12, 2013

May I ask what the rational behind removing curly braces is? I understand they are technically optional, but they do help with readability and potentially preventing bugs if less experienced people work on the code.

@isaacs

isaacs Dec 12, 2013

Owner

It's just less visual noise, and vim handles it nicely. Plus, style attachment rots your brain, so changing it up every so often is healthy :)

In this case, less-experienced people working with this code will be doing so with my supervision, so there's no real danger.

this._lengthCalculator = naiveLength
- }
-
- if (!this._max || !(typeof this._max === "number") || this._max <= 0 ) {
- // a little bit silly. maybe this should throw?
- this._max = Infinity
- }
this._allowStale = options.stale || false
-
this._maxAge = options.maxAge || null
-
this._dispose = options.dispose
-
- this._cache = Object.create(null) // hash of items by key
- this._lruList = Object.create(null) // list of items in order of use recency
- this._mru = 0 // most recently used
- this._lru = 0 // least recently used
- this._length = 0 // number of items in the list
- this._itemCount = 0
+ this.reset()
}
// resize the cache when the max changes.
@@ -134,16 +121,17 @@ LRUCache.prototype.values = function () {
}
LRUCache.prototype.reset = function () {
- if (this._dispose) {
+ if (this._dispose && this._cache) {
for (var k in this._cache) {
this._dispose(k, this._cache[k].value)
}
}
- this._cache = Object.create(null)
- this._lruList = Object.create(null)
- this._lru = 0
- this._mru = 0
- this._length = 0
+
+ this._cache = Object.create(null) // hash of items by key
+ this._lruList = Object.create(null) // list of items in order of use recency
+ this._mru = 0 // most recently used
+ this._lru = 0 // least recently used
+ this._length = 0 // number of items in the list
this._itemCount = 0
}
@@ -253,12 +241,12 @@ function del (self, hit) {
}
// classy, since V8 prefers predictable objects.
-function Entry (key, value, lu, len, age) {
+function Entry (key, value, lu, length, now) {
this.key = key
this.value = value
this.lu = lu
- this.length = len
- this.now = age
+ this.length = length
+ this.now = now
}
})()

0 comments on commit c62cc2e

Please sign in to comment.