Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lru cache FIXED 99 percent hit ratio
(version: 0)
Lru clock 2 hand version
Comparing performance of:
Test1 vs Test2
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Test1
"use strict"; let Lru = function(cacheSize,callbackBackingStoreLoad,elementLifeTimeMs=1000){ let me=this; let maxWait = elementLifeTimeMs; let size = parseInt(cacheSize,10); let mapping = {}; let buf = []; for(let i=0;i<size;i++) { let rnd = Math.random(); mapping[rnd] = i; buf.push({data:"",visited:false, key:rnd, time:Date.now()}); } let ctr= 0; let ctrEvict= parseInt(cacheSize/2,10); let loadData = callbackBackingStoreLoad; this.get = function(key){ if(key in mapping) { if(Date.now() - buf[mapping[key]].time > maxWait) { delete mapping[key]; return me.get(key); } else { buf[mapping[key]].visited=true; buf[mapping[key]].time = Date.now(); return buf[mapping[key]].data; } } else { let ctrFound = -1; while(ctrFound===-1) { if(buf[ctr].visited) { buf[ctr].visited=false; } ctr++; if(ctr >= size) { ctr=0; } if(!(buf[ctrEvict].visited)) { ctrFound = ctrEvict; } ctrEvict++; if(ctrEvict >= size) { ctrEvict=0; } } delete mapping[buf[ctrFound].key]; mapping[key] = ctrFound; let dataKey = loadData(key); buf[ctrFound] = {data:dataKey, visited:false, key:key,time:Date.now()}; return buf[ctrFound].data; } }; }; let lru = new Lru(99,function(key){ /* cache miss, load from data-store */ let wait=Date.now();let aa=0; while(Date.now()-wait<1){aa++;} return aa.toString()+key; },5000 /*miliseconds before next get() invalidates data */); let c=0; let tt=Date.now(); function runThis() { c++; let myData = lru.get(parseInt(Math.random()*10,10).toString()+"ee"); if(c<1000) runThis(); else console.log(Date.now()-tt); } runThis();
Test2
async function runThis() { for(let i=0;i<1000;i++) { let wait=Date.now();let aa=0; while(Date.now()-wait<1){aa++;} console.log(parseInt(Math.random()*10,10)); } } runThis();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test1
Test2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of an LRU (Least Recently Used) cache implementation in JavaScript. The cache has a fixed size of 99 elements and a maximum wait time of 5 seconds for invalidation. The cache uses a clock-based eviction strategy, where the most recently used item is evicted first when the cache is full. The `get` method retrieves an item from the cache by its key, and if the item is not found, it loads data from a backing store using the provided callback function. **Script Preparation Code** There is no script preparation code provided for this benchmark. This means that the test cases are self-contained and do not rely on any external setup or dependencies. **Test Cases** There are two test cases: 1. **Test1**: This test case measures the performance of the LRU cache implementation by repeatedly retrieving random keys from the cache using the `get` method. The test case runs for 1000 iterations, and the number of executions per second is measured. 2. **Test2**: This test case measures the performance of an asynchronous function that repeatedly logs a random integer to the console. The test case runs for 1000 iterations, and the number of executions per second is measured. **Library and Features** There are no external libraries used in this benchmark. However, it uses some special JavaScript features: * `let` and `const` declarations are used to declare variables. * Arrow functions (`=>`) are used as callbacks for the `get` method. * The `Date.now()` function is used to get the current timestamp. **Options Compared** The two test cases compare different approaches to measuring performance: 1. **Test1**: This test case measures the performance of the LRU cache implementation by retrieving random keys from the cache using the `get` method. It runs for 1000 iterations and measures the number of executions per second. 2. **Test2**: This test case measures the performance of an asynchronous function that logs a random integer to the console. It runs for 1000 iterations and measures the number of executions per second. **Pros and Cons** The pros and cons of these approaches are: * **Test1**: + Pros: Measures the performance of the LRU cache implementation in a realistic scenario. + Cons: May be affected by caching effects, where the results may vary depending on the order of operations. * **Test2**: + Pros: Isolates the performance measurement from any potential caching effects. + Cons: May not accurately represent real-world scenarios, as it only logs integers to the console. **Other Considerations** Other considerations for this benchmark include: * The use of a fixed-size LRU cache may not be representative of real-world scenarios, where caches are often dynamically resized or have different eviction strategies. * The clock-based eviction strategy used in this benchmark may not be suitable for all use cases, such as caching data that is frequently updated. **Alternative Approaches** Other approaches to measuring the performance of the LRU cache implementation could include: * Measuring the performance of a simulated cache with varying access patterns. * Using a different eviction strategy, such as time-to-live (TTL) or probability-based eviction. * Comparing the performance of the LRU cache implementation with other caching algorithms, such as LFU (Least Frequently Used) or MRU (Most Recently Used).
Related benchmarks:
Rounding methods
roundDecimal() with Math.pow vs. Unary and toPrecision()
toFixed vs toPrecision vs Math.round() feat. Math.pow
number floor format1
toFixed vs Math.round() sd6f54sd6f54
Comments
Confirm delete:
Do you really want to delete benchmark?