tiny-lru is a high-performance, lightweight LRU (Least Recently Used) cache library for JavaScript with O(1) operations and optional TTL support.
npm install # Install dependencies
npm run build # Lint and build (runs lint then rollup)
npm run rollup # Build with rollup
npm run test # Run lint and tests
npm run coverage # Run tests with coverage reporting
npm run fix # Fix linting and formatting issues
npm run lint # Lint code with oxlintSource code is in src/.
- lint: Check and fix linting issues with oxlint
- fix: Fix linting and formatting issues
- build: Lint + rollup build
- coverage: Run test suite with coverage reporting
├── src/lru.js # Main LRU cache implementation
├── tests/ # Test files
├── benchmarks/ # Performance benchmarks
├── dist/ # Built distribution files
│ ├── tiny-lru.js # ES Modules
│ ├── tiny-lru.cjs # CommonJS
│ └── tiny-lru.min.js # Minified ESM
├── types/ # TypeScript definitions
├── docs/ # Documentation
├── rollup.config.js # Build configuration
└── package.json # Project configuration
- Indentation: Tabs
- Quotes: Double quotes
- Semicolons: Required
- Array constructor: Avoid
new Array()(oxlint will warn)
- DRY (Don't Repeat Yourself): Extract common logic into reusable functions; avoid duplication
- YAGNI (You Ain't Gonna Need It): Implement only what's needed; avoid over-engineering
- SOLID: Follow single responsibility, open/closed, and interface segregation principles
- OWASP: Prioritize security; avoid unsafe operations, validate inputs, prevent injection risks
lru(max, ttl, resetTtl)- Factory function to create cacheLRUclass - Direct instantiation withnew LRU(max, ttl, resetTtl)- Core methods:
get(),set(),peek(),delete(),has(),clear(),evict() - Array methods:
keys(),values(),entries() - Utility methods:
forEach(),getMany(),hasAll(),hasAny(),cleanup(),toJSON(),stats(),onEvict(),sizeByTTL(),keysByTTL(),valuesByTTL() - Properties:
first,last,max,size,ttl,resetTtl peek(key)- Retrieve value without moving it (no LRU update, no TTL check)
- Framework: Node.js built-in test runner (
node --test) - Tests: 149 tests across 26 suites
- Coverage: 100% lines, 99.28% branches, 100% functions
- Test pattern:
tests/**/*.js - All tests must pass with 100% line coverage before merging
- Run:
npm test(lint + tests) ornpm run coveragefor coverage report
- Memory leaks: When removing items from the linked list, always clear
prev/nextpointers to allow garbage collection - LRU order pollution: Methods like
entries()andvalues()should access items directly rather than callingget(), which moves items and can delete expired items mid-iteration - TTL edge cases: Direct property access (
this.items[key]) should be used instead ofhas()when you need to inspect expired-but-not-yet-deleted items - Dead code: Always verify edge case code is actually reachable before adding special handling
- Constructor assignment: Use
letnotconstfor variables that may be reassigned (e.g., insetWithEvicted)
- The LRU uses a doubly-linked list with
firstandlastpointers for O(1) operations - TTL is stored per-item as an
expirytimestamp;0means no expiration moveToEnd()is the core method for maintaining LRU order - item is always moved to thelastpositionsetWithEvicted()optimizes updates by avoiding the fullset()path for existing keys