Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lru cache non-async version with 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
Script Preparation code:
"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; } }; }; document.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 */);
Tests:
Test1
let c=0; let tt=Date.now(); function runThis() { for(let i=0;i<10000;i++) { let myData = document.lru.get(parseInt(Math.random()*100,10).toString()+"ee"); console.log(myData); } } runThis();
Test2
async function runThis() { for(let i=0;i<10000;i++) { let wait=Date.now();let aa=0; while(Date.now()-wait<1){aa++;} console.log(parseInt(Math.random()*100,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):
I'll provide an explanation of the benchmark, its options, pros and cons, library usage, and special JS features. **Benchmark Overview** The benchmark measures the performance of a Least Recently Used (LRU) cache implementation with a 99% hit ratio. The benchmark consists of two test cases: "Test1" and "Test2". Both tests access the LRU cache 10,000 times using randomly generated keys. **Options Compared** The benchmark compares two approaches: 1. **Synchronous**: Test1 uses a synchronous function to access the cache. It generates random keys, accesses the cache, and logs the result. 2. **Asynchronous**: Test2 uses an asynchronous function to access the cache. However, instead of actually waiting for any operation, it simply increments a counter until 1 second has passed. **Pros and Cons** **Synchronous Approach (Test1)** Pros: * Simulates real-world usage where cache accesses are typically synchronous. * Allows for accurate measurement of cache hit rates and access times. Cons: * Can be slower due to the overhead of waiting for cache misses. * May not accurately represent asynchronous code that waits for I/O operations. **Asynchronous Approach (Test2)** Pros: * Faster execution time, as it doesn't wait for cache misses. * Simulates asynchronous code that waits for I/O operations. Cons: * Doesn't accurately represent real-world usage where cache accesses are synchronous. * May not provide a fair comparison with the synchronous approach. **Library Usage** The benchmark uses a custom LRU cache implementation written in JavaScript. The `Lru` function takes three arguments: `cacheSize`, `callbackBackingStoreLoad`, and an optional `elementLifeTimeMs`. The `callbackBackingStoreLoad` argument is called when a cache miss occurs, allowing for loading data from a backing store. **Special JS Features** None of the provided benchmark code uses any special JavaScript features beyond the standard language syntax. **Alternatives** Other alternatives to measure LRU cache performance include: * Using an existing caching library or framework (e.g., Redis, Memcached). * Implementing a custom LRU cache using a different data structure (e.g., array, linked list). * Measuring cache performance using other benchmarking tools or frameworks. Keep in mind that the choice of alternative will depend on the specific requirements and constraints of the project.
Related benchmarks:
Lru cache
Lru cache 50% hit ratio test
Lru cache non-async version: time complexity of cache-hits
Lru cache non-async version: time complexity of cache-miss
Comments
Confirm delete:
Do you really want to delete benchmark?