node-modules/bench-lru

Name: bench-lru

Owner: node_modules

Description: null

Forked from: dominictarr/bench-lru

Created: 2016-12-29 07:24:42.0

Updated: 2016-12-29 07:24:43.0

Pushed: 2016-12-29 12:32:52.0

Homepage: null

Size: 13

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

bench-lru

benchmark the least-recently-used caches which are available on npm.

Introduction

An LRU cache is a cache with bounded memory use. The point of a cache is to improve performance, so how performant are the available implementations?

LRUs achive bounded memory use by removing the oldest items when a threashold number of items is reached. We measure 3 cases, adding an item, updating an item, and adding items which push other items out of the LRU.

There is a previous benchmark but it did not describe it's methodology. (and since it measures the memory used, but tests everything in the same process, it does not get clear results)

Benchmark

I run a very simple benchmark. In four phases:

  1. set the LRU to fit max N=100,000 items.
  2. add N random numbers to the cache, with keys 0-N.
  3. then update those keys with new random numbers.
  4. then evict those keys, by adding keys N-2N.
Results

Operations per millisecond (higher is better):

| name | set | get1 | update | get2 | evict | | —————– | ——- | ——– | ——— | ——– |——– | | hashlru | 6250 | 8333 | 6250 | 7143 | 4167 | | lru-native | 714 | 1053 | 935 | 1042 | 510 | | lru-cache | 300 | 1449 | 592 | 1786 | 247 | | modern-lru | 239 | 581 | 498 | 578 | 370 | | lru_cache | 3226 | 14286 | 11111 | 12500 | 22 | | lru-fast | 1493 | 4762 | 12500 | 14286 | 7 | | simple-lru-cache | 3448 | 6667 | 6667 | 9091 | 7 | | lru | 1887 | 2778 | 1724 | 2857 | 7 | | secondary-cache | 935 | 7 | 3 | 2 | 1 | | mkc | 410 | 7 | 3 | 2 | 1 | | faster-lru-cache | 1 | 1 | 1 | 1 | 1 |

We can group the results in a few categories:

Discussion

It appears that all-round performance is the most difficult to achive, in particular, performance on eviction is difficult to achive. I think eviction performance is the most important consideration, because once the cache is warm each subsequent addition causes an eviction, and actively used, hot, cache will run close to it's eviction performance. Also, some have faster add than update, and some faster update than add.

modern-lru gets pretty close to lru-native perf. I wrote hashlru after my seeing the other results from this benchmark, it's important to point out that it does not use the classic LRU algorithm, but has the important properties of the LRU (bounded memory use and O(1) time complexity)

Future work

This is still pretty early results, take any difference smaller than an order of magnitude with a grain of salt.

It is necessary to measure the statistical significance of the results to know accurately the relative performance of two closely matched implementations.

I also didn't test the memory usage. This should be done running the benchmarks each in a separate process, so that the memory used by each run is not left over while the next is running.

conclusion

javascript is generally slow, so one of the best ways to make it fast is to write less of it. LRUs are also quite difficult to implement (linked lists!). In trying to come up with a faster LRU implementation I realized that something far simpler could do the same job. Especially given the strengths and weaknesses of javascript, this is significantly faster than any of the other implementations, including the C implementation. Likely, the overhead of the C<->js boundry is partly to blame here.

Changelog
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.