npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sonic-memoize

v1.1.3

Published

Ultra fast, zero config memoization of functions

Downloads

419

Readme

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 Name | Average Time (ps) | | -------------- | ----------------- | | sonic-memoize | 8.5 | | mem | 11.2 | | lodash.memoize | 22.2 | | nano-memoize | 43.8 | | fast-memoize | 279.3 | | memoizee | 410.5 |

Results for function with single parameter of type number:

| Task Name | Average Time (ps) | | -------------- | ----------------- | | nano-memoize | 4.4 | | sonic-memoize | 4.4 | | fast-memoize | 4.7 | | mem | 5.1 | | lodash.memoize | 29.3 | | memoizee | 385.3 |

Results for function with single non primitive parameter:

| Task Name | Average Time (ps) | | -------------- | ----------------- | | sonic-memoize | 4.4 | | mem | 6.7 | | lodash.memoize | 19.6 | | nano-memoize | 40.2 | | memoizee | 193.7 | | fast-memoize | 372.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 Name | Average Time (ps) | | ------------- | ----------------- | | sonic-memoize | 41.0 | | memoizee | 84.5 | | fast-memoize | 464.5 | | nano-memoize | 1550.5 |

Results for function with multiple non primitive parameters:

| Task Name | Average Time (ps) | | ------------- | ----------------- | | sonic-memoize | 46.2 | | memoizee | 242.6 | | fast-memoize | 788.2 | | nano-memoize | 3049.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 Name | Average Time (ps) | | ------------------- | ----------------- | | sonic-memoize (lru) | 30.1 | | memoizerific | 1236.1 | | memoizee (lru) | 1754.9 | | moize | 7598.9 | | micro | 8030.6 |

Results for function with single parameter of type number:

| Task Name | Average Time (ps) | | ------------------- | ----------------- | | sonic-memoize (lru) | 16.0 | | memoizee (lru) | 692.3 | | memoizerific | 1257.8 | | moize | 4978.3 | | micro | 8117.5 |

Results for function with single non primitive parameter:

| Task Name | Average Time (ps) | | ------------------- | ----------------- | | sonic-memoize (lru) | 16.0 | | memoizee (lru) | 501.6 | | memoizerific | 1172.7 | | micro | 8872.3 | | moize | 8897.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 Name | Average Time (ps) | | ------------------- | ----------------- | | sonic-memoize (lru) | 51.5 | | memoizee (lru) | 419.2 | | memoizerific | 1129.6 | | moize | 6083.2 | | micro | 10822.4 |

Results for function with multiple non primitive parameters:

| Task Name | Average Time (ps) | | ------------------- | ----------------- | | sonic-memoize (lru) | 56.2 | | memoizee (lru) | 563.9 | | memoizerific | 1122.1 | | micro | 9310.0 | | moize | 9521.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