Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

optimization: Don't iterate over deleted keys

  • Loading branch information...
commit 780488b946e5d768b5dc0efed5072f60a09a4f93 1 parent d4ed7ea
@isaacs authored
Showing with 5 additions and 3 deletions.
  1. +5 −3 lib/lru-cache.js
View
8 lib/lru-cache.js
@@ -102,7 +102,9 @@ function LRUCache (options) {
this.forEach = function (fn, thisp) {
thisp = thisp || this
- for (var k = mru - 1; k >= 0; k--) if (lruList[k]) {
+ var i = 0;
+ for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) {
+ i++
var hit = lruList[k]
fn.call(thisp, hit.value, hit.key, this)
}
@@ -111,7 +113,7 @@ function LRUCache (options) {
this.keys = function () {
var keys = new Array(itemCount)
var i = 0
- for (var k = mru - 1; k >= 0; k--) if (lruList[k]) {
+ for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) {
var hit = lruList[k]
keys[i++] = hit.key
}
@@ -121,7 +123,7 @@ function LRUCache (options) {
this.values = function () {
var values = new Array(itemCount)
var i = 0
- for (var k = mru - 1; k >= 0; k--) if (lruList[k]) {
+ for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) {
var hit = lruList[k]
values[i++] = hit.value
}
Please sign in to comment.
Something went wrong with that request. Please try again.