I solved this one without using the set timeout callback, i just enter both the value and a expiration in an object as value. I do this live as well and have a periodic cleanup of expired cache items
Object.keys(obj): This method internally iterates over all the enumerable properties of the object to construct an array of keys. Therefore, its time complexity is O(n), where n is the number of enumerable properties in the object. .length: Accessing the length of an array is a constant-time operation, O(1). Due to the structure of accessing the length; it would require JavaScript to find all the keys first and THEN calculate the length; so this solution would result in iterating through all the keys, making it not efficient for this question.
*But, if you wanted, you could just make an extra "length" property in the class and update that property everytime a new key is added, and delete whenever the timeout function is executed This way, you could just access the length anytime (which would be in constant time)
I’ve only recently learned js (still learning) and I’m reluctant to use classes even though I came from a Java background. I almost always go with arrow functions but should I go with classes more?
Hi, I have seen people bind 'this' in the constructor like this.get=this.get.bind('this'). But you don't do that and solution gets accepted either way. So can you explain what it does ?
Would've uploaded this earlier but someone broke youtube last night (was NOT me 😉)
twitter.com/TeamRUclips/status/1659060023736016896
Really appreciate the explanation of the js problem series
The variety of stuff you can do here makes me wanna like Javascript
Watched twice to fully understand. Thanks!
Thx. i like the class sol much butter than the original one😃
This pattern is very useful for something like an API cache
I solved this one without using the set timeout callback, i just enter both the value and a expiration in an object as value. I do this live as well and have a periodic cleanup of expired cache items
Was waiting for this
wow, finally! thanks for this ^_^
we can also do Object.keys(obj).length;
if using object instead of map
Object.keys(obj): This method internally iterates over all the enumerable properties of the object to construct an array of keys. Therefore, its time complexity is O(n), where n is the number of enumerable properties in the object.
.length: Accessing the length of an array is a constant-time operation, O(1).
Due to the structure of accessing the length; it would require JavaScript to find all the keys first and THEN calculate the length; so this solution would result in iterating through all the keys, making it not efficient for this question.
*But, if you wanted, you could just make an extra "length" property in the class and update that property everytime a new key is added, and delete whenever the timeout function is executed
This way, you could just access the length anytime (which would be in constant time)
Thanks for your sharing~
I truly hate and love JS at the same time.
I’ve only recently learned js (still learning) and I’m reluctant to use classes even though I came from a Java background. I almost always go with arrow functions but should I go with classes more?
Thank you!
Finally a easy one.
Day 14 of doing the 30-day challenge with neetcode
hehehe finally :p
You speak very fast. Is this your habit? My mother tongue is not English and it is difficult to understand you. Nevertheless I appreciate for videos.
Спасибо, брат
Hi, I have seen people bind 'this' in the constructor like this.get=this.get.bind('this'). But you don't do that and solution gets accepted either way. So can you explain what it does ?
Great content! There is a change that would be nice in the get method: return this.cache.get(key)?.value ?? -1;