Skip to content

Commit ea96d6f

Browse files
committed
Changes made as asked
1 parent 477c8bd commit ea96d6f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/ImageCache.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ assign(Img.prototype, EventEmitter.prototype, {
7777
});
7878

7979
var kInstancePoolLength = 300;
80+
8081
var _instancePool = {
8182
length: 0,
8283
// Keep all the nodes in memory.
@@ -115,18 +116,17 @@ var _instancePool = {
115116
},
116117

117118
_reduceLeastUsed: function (least, currentHash) {
118-
var current = this.elements[currentHash];
119+
var current = _instancePool.elements[currentHash];
119120

120-
if( least.freq > current.freq ){
121-
return current;
122-
}
121+
if( least.freq > current.freq ){
122+
return current;
123+
}
123124

124-
return least;
125+
return least;
125126
},
126127

127128
popLeastUsed: function () {
128-
// there is a much faster way using self
129-
var reducer = this._reduceLeastUsed.bind(this);
129+
var reducer = _instancePool._reduceLeastUsed;
130130
var minUsed = Object.keys(this.elements).reduce(reducer, { freq: Infinity });
131131

132132
if( minUsed.hash ){
@@ -149,7 +149,7 @@ var ImageCache = {
149149
get: function (src) {
150150
var image = _instancePool.get(src);
151151
if (!image) {
152-
// Simple FIFO queue
152+
// Awesome LRU
153153
image = new Img(src);
154154
if (_instancePool.length >= kInstancePoolLength) {
155155
_instancePool.popLeastUsed().destructor();
@@ -161,4 +161,4 @@ var ImageCache = {
161161

162162
};
163163

164-
module.exports = ImageCache;
164+
module.exports = ImageCache;

0 commit comments

Comments
 (0)