let cache = {};
async function getData(url){
let result = "";
if(cache[url] !== undefined) return cache[url].value;
await fetch(url...
For further actions, you may consider blocking this person and/or reporting abuse
Just out of curiosity why are you calling:
inside of .forEach? Why not assign it before the call:
this way you only pay for the overhead of calling Date once.
I expect new Date() to be practically a constant (maybe 100ms difference) over the time spent clearing the cache. Since every three seconds you are clearing anything in the cache older than 10 seconds.
Calling new Date() IIUC is expensive since it has to reach down to the operating system to get the date value.
True some item might linger in the cache for 100 ms longer but....
Good correction, I will update the code.
Also I didn't write it to be optimised. I was just experimenting how caching works simply and pasted same code here 😀.
It's good that you shared interesting material, but remember that not all developers know, are thinking and/or working on optimizing their scripts. There are beginners, and perhaps not only beginners, who, seeing interesting code at first glance, copy it into their applications and use it without checking or optimizing, and then other people may suffer from sudden errors that can come out absolutely by accident.
It is necessary to approach operations related to cycles carefully and try to think through the work of the function in advance, since even any small error can entail a bunch of others that can cause unnecessary memory costs or something else, perhaps. Lol.
I wish you good luck.
Sure brother.
Can I ask you why are you doing this:
Instead of this:
If you use await you can replace those .then and I think this is much more cleaner :D
Good Improvement. I will update the code.
I think you can append .json() to this:
(I didn't test it but I think it should work)
Happy to help you! :D
No it cannot be done.
Actually that doesn't work and you'll get an error.
Uncaught TypeError: fetch(...).json is not a function
Because
fetch()
return a promise, and it doesn't have ajson
method on it.It should be like this.
or this
Note that calling
response.json()
also return a promise so you need toawait
it to get the result.Note: if you need caching for production projects, I highly recommend searching for existing implementations (or at least existing algorithms).
Otherwise I have some notes:
cache[key].time
with a validUntil time object.restep++
Nice post, thanks for sharing.
What do you think about integrating the clearing of the cache in the getData function?
Performance wise it should be very similar and the result is even more compact (just 10 lines 😄)
➕➕
Nice idea to use Memoized results.Thanks!!
First rule of software development: see if it's been done before - don't reinvent the wheel. Check out the Cache interface, which would provide you with caching across browser sessions: developer.mozilla.org/en-US/docs/W...
Chill out man, Its just an experiment of curiosity which I felt good to share.
Great job. Thanks for sharing.
👍👍
Nice post
Thanks @aaravrrrrrr
Cool!
What happen if you get the same data at the same url very closely, the first call didn't finished so you make two calls instead of one
Didn't understand much, can you please explain a bit more.
Thanks, great read.