Permalink
Browse files

Use null-prototype'd objects as bags o' stuff

  • Loading branch information...
1 parent 038f537 commit a02705c874addf8aebc3e6cfe682a0a852887dbd @isaacs committed Sep 13, 2012
Showing with 8 additions and 9 deletions.
  1. +8 −9 lib/lru-cache.js
View
@@ -40,8 +40,8 @@ function LRUCache (options) {
var dispose = options.dispose
- var cache = {} // hash of items by key
- , lruList = {} // list of items in order of use recency
+ var cache = Object.create(null) // hash of items by key
+ , lruList = Object.create(null) // list of items in order of use recency
, mru = 0 // most recently used
, length = 0 // number of items in the list
, itemCount = 0
@@ -66,16 +66,16 @@ function LRUCache (options) {
if (typeof lC !== "function") {
lengthCalculator = naiveLength
length = itemCount
- Object.keys(cache).forEach(function (key) {
+ for (var key in cache) {
cache[key].length = 1
- })
+ }
} else {
lengthCalculator = lC
length = 0
- Object.keys(cache).forEach(function (key) {
+ for (var key in cache) {
cache[key].length = lengthCalculator(cache[key].value)
length += cache[key].length
- })
+ }
}
if (length > max) trim()
@@ -97,9 +97,9 @@ function LRUCache (options) {
this.reset = function () {
if (dispose) {
- Object.keys(cache).forEach(function (k) {
+ for (var k in cache) {
dispose(k, cache[k].value)
- })
+ }
}
cache = {}
lruList = {}
@@ -173,7 +173,6 @@ function LRUCache (options) {
if (length <= max) return
for (var k in lruList) {
if (length <= max) break;
- if (!hOP(lruList, k)) continue; // protect against wild savages
var hit = lruList[k]
if (dispose) dispose(hit.key, hit.value)
length -= hit.length

0 comments on commit a02705c

Please sign in to comment.