Oleksii Trekhleb | Javascript algorithms (Least recently used (LRU) cache)

This is a series of books diving deep into the core mechanisms of the JavaScript language.

 · 2 phút đọc.

This is a series of books diving deep into the core mechanisms of the JavaScript language.

A Least Recently Used (LRU) Cache organizes items in order of use, allowing you to quickly identify which item hasn’t been used for the longest amount of time.

Picture a clothes rack, where clothes are always hung up on one side. To find the least-recently used item, look at the item on the other end of the rack.

The problem statement

Implement the LRUCache class:

  • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  • int get(int key) Return the value of the key if the key exists, otherwise return undefined.
  • void set(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.

The functions get() and set() must each run in O(1) average time complexity.

Implementation

Version 1: Doubly Linked List + Hash Map

See the LRUCache implementation example in LRUCache.js. The solution uses a HashMap for fast O(1) (in average) cache items access, and a DoublyLinkedList for fast O(1) (in average) cache items promotions and eviction (to keep the maximum allowed cache capacity).

Linked List

Made with okso.app

You may also find more test-case examples of how the LRU Cache works in LRUCache.test.js file.

Version 2: Ordered Map

The first implementation that uses doubly linked list is good for learning purposes and for better understanding of how the average O(1) time complexity is achievable while doing set() and get().

However, the simpler approach might be to use a JavaScript Map object. The Map object holds key-value pairs and remembers the original insertion order of the keys. We can use this fact in order to keep the recently-used items in the end of the map by removing and re-adding items. The item at the beginning of the Map is the first one to be evicted if cache capacity overflows. The order of the items may checked by using the IterableIterator like map.keys().

See the LRUCacheOnMap implementation example in LRUCacheOnMap.js.

You may also find more test-case examples of how the LRU Cache works in LRUCacheOnMap.test.js file.

Complexities

Average
SpaceO(n)
Get itemO(1)
Set itemO(1)
nhavantuonglai

Share:
Quay lại.

Có thể bạn chưa đọc

Xem tất cả »

Đăng ký nhận bảng tin hàng tuần

Liên lạc trao đổi

Liên lạc thông qua Instagram

Thông qua Instagram, bạn có thể trao đổi trực tiếp và tức thời, cũng như cập nhật những thông tin mới nhất từ nhavantuonglai.

Tức thời

Bạn có thể gửi và nhận tin nhắn nhanh chóng, trực tiếp, giúp những vấn đề cá nhân của bạn được giải quyết tức thời và hiệu quả hơn.

Thân thiện

Vì tính chất là kênh liên lạc nhanh, nên bạn có thể bỏ qua những nghi thức giao tiếp thông thường, chỉ cần lịch sự và tôn trọng thì sẽ nhận được sự phản hồi đầy thân thiện, thoải mái từ tác giả.

Trao đổi trên email

Thông qua email cá nhân, bạn có thể trao đổi thỏa thuận hợp tác, kết nối chuyên sâu và mang tính chuyên nghiệp.

Tin cậy

Trong một số trường hợp, email được dùng như một tài liệu pháp lý, chính vì vậy mà bạn có thể an tâm và tin cậy khi trao đổi với tác giả thông qua email.

Chuyên nghiệp

Cấu trúc của email đặt tính chuyên nghiệp lên hàng đầu, nên những thông tin, nội dung được viết trong email từ tác giả sẽ luôn đảm bảo điều này ở mức cao nhất.