1.1.3 • Published 1 year ago

sonic-memoize v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

sonic-memoize

Ultra fast, zero config memoization of functions.

npm install sonic-memoize

Usage

import memoize from "sonic-memoize";

const memoized = memoize(function (arg1, arg2, arg3) {
  // expensive computation here
});

memoized(3, 4, 5); // First call with these arguments is expensive
memoized(3, 4, 5); // Cache hit, the value was returned instantly

sonic-memoize is zero config and will memoize your function calls out of the box without limitation on how many calls with different arguments will be cached. If you need to set a max size on the cache that is being used by sonic-memoize, see the section on setting a size limit on the cache

Benchmarks

We implemented benchmarking with realistic test cases that should give you a good impression on how well the memoization will perform in real life situations.

Benchmarking memoization with one function parameter without Cache size limit:

Results for function with single parameter of type string:

Task NameAverage Time (ps)
sonic-memoize8.5
mem11.2
lodash.memoize22.2
nano-memoize43.8
fast-memoize279.3
memoizee410.5

Results for function with single parameter of type number:

Task NameAverage Time (ps)
nano-memoize4.4
sonic-memoize4.4
fast-memoize4.7
mem5.1
lodash.memoize29.3
memoizee385.3

Results for function with single non primitive parameter:

Task NameAverage Time (ps)
sonic-memoize4.4
mem6.7
lodash.memoize19.6
nano-memoize40.2
memoizee193.7
fast-memoize372.1

The benchmarks were being run on sample sets of 1000 different arguments repeatedly. That makes for a realistic scenario. benchmarking code

Benchmarking memoization with multiple function parameters without Cache size limit:

Results for function with multiple primitive parameters:

Task NameAverage Time (ps)
sonic-memoize41.0
memoizee84.5
fast-memoize464.5
nano-memoize1550.5

Results for function with multiple non primitive parameters:

Task NameAverage Time (ps)
sonic-memoize46.2
memoizee242.6
fast-memoize788.2
nano-memoize3049.0

The benchmarks were being run on sample sets of 1000 different arguments repeatedly. That makes for a realistic scenario. We excluded memoization libraries that cannot memoize functions with multiple arguments at all or without further configuration. benchmarking code

Setting a size limit on the cache

You can use sonic-memoize with the option of limiting the internal cache size to a specific number of cached results. sonic-memoize will use an LRU caching strategy, which means that the least recently used function call will be deleted from the cache if the max size is reached. Note that limiting the size of the cache comes at a (small) performance cost and is probably unnecessary if you dont have tough memory usage restrictions or call your memoized functions with many millions of different arguments.

import { memoizeWithLimit } from "sonic-memoize";

const memoized = memoizeWithLimit(function (arg1, arg2, arg3) {
  // expensive computation here
}, 50000); // The second argument (50000 in this case) specifies the cache size limit

// After 50000 calls with different arguments sonic-memoize will start
// deleting the least recently used function call from cache

memoized(3, 4, 5); // First call with these arguments is expensive
memoized(3, 4, 5); // Cache hit, the value was returned instantly

Benchmarks on LRU memoization

See below benchmarks with different memoization libraries that use LRU caching strategies.

Benchmarking memoization with one function parameter with LRU Cache size limit:

Results for function with single parameter of type string:

Task NameAverage Time (ps)
sonic-memoize (lru)30.1
memoizerific1236.1
memoizee (lru)1754.9
moize7598.9
micro8030.6

Results for function with single parameter of type number:

Task NameAverage Time (ps)
sonic-memoize (lru)16.0
memoizee (lru)692.3
memoizerific1257.8
moize4978.3
micro8117.5

Results for function with single non primitive parameter:

Task NameAverage Time (ps)
sonic-memoize (lru)16.0
memoizee (lru)501.6
memoizerific1172.7
micro8872.3
moize8897.8

The benchmarks were being run on sample sets of 1000 different arguments repeatedly, while the size limits of the caches were set to 1000 as well. That makes for a realistic scenario in which the cache size is big enough to handle most function calls. benchmarking code

Benchmarking memoization with multiple function parameters with LRU Cache size limit:

Results for function with multiple primitive parameters:

Task NameAverage Time (ps)
sonic-memoize (lru)51.5
memoizee (lru)419.2
memoizerific1129.6
moize6083.2
micro10822.4

Results for function with multiple non primitive parameters:

Task NameAverage Time (ps)
sonic-memoize (lru)56.2
memoizee (lru)563.9
memoizerific1122.1
micro9310.0
moize9521.1

The benchmarks were being run on sample sets of 1000 different arguments repeatedly, while the size limits of the caches were set to 1000 as well. That makes for a realistic scenario in which the cache size is big enough to handle most function calls. benchmarking code

License

MIT

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago