-
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding LRU cache to make Range lookups much faster #124
Open
ludu12
wants to merge
3
commits into
fabiooshiro:main
Choose a base branch
from
ludu12:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
Performance benchmarking script for Range.js | ||
*/ | ||
|
||
const XLSX = require("xlsx"); | ||
const XLSX_CALC = require("../src"); | ||
const workbook = XLSX.readFile(`${__dirname}/vlookup_large_range.xlsx`); | ||
|
||
const times = [] | ||
const n = 100; | ||
|
||
for (let i = 0; i < n; i++) { | ||
const t0 = performance.now(); | ||
XLSX_CALC(workbook); | ||
const t1 = performance.now(); | ||
const previous = t1 - t0; | ||
times.push(previous) | ||
} | ||
|
||
const average = (times.reduce((sum, t) => sum + t, 0)) / n; | ||
console.log(`Average time for ${n} executions: ${average.toFixed(2)} ms`) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class LRUCache { | ||
constructor(capacity = 500) { | ||
this.cache = new Map(); | ||
this.capacity = capacity; | ||
} | ||
|
||
clear() { | ||
this.cache = new Map(); | ||
} | ||
|
||
get(key) { | ||
if (!this.cache.has(key)) return null; | ||
|
||
let val = this.cache.get(key); | ||
|
||
this.cache.delete(key); | ||
this.cache.set(key, val); | ||
|
||
return val; | ||
} | ||
|
||
set(key, value) { | ||
this.cache.delete(key); | ||
|
||
if (this.cache.size === this.capacity) { | ||
this.cache.delete(this.cache.keys().next().value); | ||
this.cache.set(key, value); | ||
} else { | ||
this.cache.set(key, value); | ||
} | ||
} | ||
} | ||
|
||
module.exports = LRUCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use strict"; | ||
const LRUCache = require('../src/LRUCache.js'); | ||
const assert = require('assert'); | ||
|
||
describe('LRU cache', () => { | ||
it('should return null if missing from cache', () => { | ||
const cache = new LRUCache() | ||
assert.equal(cache.get('key'), null); | ||
}); | ||
|
||
it('should cache results', () => { | ||
const cache = new LRUCache() | ||
cache.set('key', 'value') | ||
assert.equal(cache.get('key'), 'value'); | ||
}); | ||
|
||
it('should remove least recently used if at capacity', () => { | ||
const cache = new LRUCache(2) | ||
cache.set('key1', 'value1') | ||
cache.set('key2', 'value2') | ||
cache.set('key3', 'value3') | ||
|
||
// assert keys | ||
assert.equal(cache.get('key1'), null); | ||
assert.equal(cache.get('key2'), 'value2'); | ||
assert.equal(cache.get('key3'), 'value3'); | ||
}); | ||
|
||
|
||
it('should update cache when accessed', () => { | ||
const cache = new LRUCache(2) | ||
cache.set('key1', 'value1') | ||
cache.set('key2', 'value2') | ||
// accessing key 1 to update recently used | ||
cache.get('key1') | ||
|
||
cache.set('key3', 'value3') | ||
|
||
assert.equal(cache.get('key1'), 'value1'); | ||
assert.equal(cache.get('key2'), null); | ||
assert.equal(cache.get('key3'), 'value3'); | ||
}); | ||
|
||
it('should empty cache when cleared', () => { | ||
const cache = new LRUCache() | ||
cache.set('key1', 'value1') | ||
cache.set('key2', 'value2') | ||
cache.set('key3', 'value3') | ||
|
||
cache.clear(); | ||
|
||
assert.equal(cache.get('key1'), null); | ||
assert.equal(cache.get('key2'), null); | ||
assert.equal(cache.get('key3'), null); | ||
}); | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache needs to be cleared for this test.