Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.5.0
Choose a base ref
...
head repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.5.1
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Mar 14, 2022

  1. even more iteration protection

    There's really no way around checking if the iteration index is valid.
    This makes me a little bit sad, because testing for index validity is
    slow af. But I suppose cache.entries() and friends are not really likely
    to be used in performance-critical paths as much as set() and get().
    
    Re: #212
    isaacs committed Mar 14, 2022
    Copy the full SHA
    dd5b06a View commit details
  2. 7.5.1

    isaacs committed Mar 14, 2022
    Copy the full SHA
    e608eb8 View commit details
Showing with 43 additions and 7 deletions.
  1. +8 −4 index.js
  2. +2 −2 package-lock.json
  3. +1 −1 package.json
  4. +32 −0 test/delete-while-iterating.js
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -244,12 +244,14 @@ class LRUCache {
*indexes ({ allowStale = this.allowStale } = {}) {
if (this.size) {
for (let i = this.tail, j; true; ) {
if (!this.isValidIndex(i)) {
break
}
j = i === this.head
if (allowStale || !this.isStale(i)) {
yield i
}
// either head now, or WAS head and head was deleted
if (i === this.head || j && !this.isValidIndex(i)) {
if (i === this.head) {
break
} else {
i = this.prev[i]
@@ -261,12 +263,14 @@ class LRUCache {
*rindexes ({ allowStale = this.allowStale } = {}) {
if (this.size) {
for (let i = this.head, j; true; ) {
j = i === this.tail
if (!this.isValidIndex(i)) {
break
}
if (allowStale || !this.isStale(i)) {
yield i
}
// either the tail now, or WAS the tail, and deleted
if (i === this.tail || j && !this.isValidIndex(i)) {
if (i === this.tail) {
break
} else {
i = this.next[i]
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lru-cache",
"description": "A cache object that deletes the least-recently-used items.",
"version": "7.5.0",
"version": "7.5.1",
"author": "Isaac Z. Schlueter <i@izs.me>",
"keywords": [
"mru",
32 changes: 32 additions & 0 deletions test/delete-while-iterating.js
Original file line number Diff line number Diff line change
@@ -66,3 +66,35 @@ t.test('rdelete odds', t => {
t.same([...c.keys()], [4, 2, 0])
t.end()
})

t.test('delete two of them', t => {
const c = t.context
t.same([...c.keys()], [4, 3, 2, 1, 0])
for (const k of c.keys()) {
if (k === 3) {
c.delete(3)
c.delete(4)
} else if (k === 1) {
c.delete(1)
c.delete(0)
}
}
t.same([...c.keys()], [2])
t.end()
})

t.test('rdelete two of them', t => {
const c = t.context
t.same([...c.keys()], [4, 3, 2, 1, 0])
for (const k of c.rkeys()) {
if (k === 3) {
c.delete(3)
c.delete(4)
} else if (k === 1) {
c.delete(1)
c.delete(0)
}
}
t.same([...c.keys()], [2])
t.end()
})