3.3.3 • Published 2 days ago

@thi.ng/memoize v3.3.3

Weekly downloads
2,892
License
Apache-2.0
Repository
github
Last release
2 days ago

memoize

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Function memoization with configurable caching.

This package provides different function memoization implementations for functions with 1 or more arguments and custom result caching using ES6 Map API like implementations. Unlike native ES6 Maps, the implementations MUST support value, not just referential, equality semantics (e.g. those provided by @thi.ng/associative) or @thi.ng/cache). The latter also support automatically pruning of memoization caches, based on different strategies. See doc strings for further details.

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/cache - In-memory cache implementations with ES6 Map-like API and different eviction strategies

Installation

yarn add @thi.ng/memoize

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/memoize"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const memoize = await import("@thi.ng/memoize");

Package sizes (gzipped, pre-treeshake): ESM: 282 bytes

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Isolated, component-local DOM updatesDemoSource
Basic rstream-gestures multi-touch demoDemoSource
rstream based spreadsheet w/ S-expression formula DSLDemoSource

API

Generated API docs

import * as m from "@thi.ng/memoize";

// (optional, for custom caching)
import { EquivMap } from "@thi.ng/associative";
import { LRUCache } from "@thi.ng/cache";

Optimized version for single arg functions

foo = m.memoize1((x) => (console.log("exec"), x * 10));
foo(1);
// exec
// 10
foo(1);
// 10

// with custom cache
foo = m.memoize1(
    (x) => (console.log("exec"), x[0] * 10),
    new EquivMap()
);

foo([1]);
// exec
// 10
foo([1]); // wouldn't work w/ native ES6 Map as cache
// 10

// use LRU cache to limit cache size
foo = m.memoize1(
    (x) => (console.log("exec"), x[0] * 10),
    new LRUCache(null, { maxlen: 3 })
);

Arbitrary args

dotProduct = m.memoize(
    (x, y) => (console.log("exec"), x[0] * y[0] + x[1] * y[1]),
    new EquivMap()
);

dotProduct([1,2], [3,4]);
// exec
// 11
dotProduct([1,2], [3,4]);
// 11

Via JSON.stringify()

dotProduct = m.memoizeJ(
    (x, y) => (console.log("exec"), x[0] * y[0] + x[1] * y[1])
);
dotProduct([1,2], [3,4]);
// exec
// 11
dotProduct([1,2], [3,4]);
// 11

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-memoize,
  title = "@thi.ng/memoize",
  author = "Karsten Schmidt",
  note = "https://thi.ng/memoize",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

3.3.3

2 days ago

3.3.2

5 days ago

3.3.1

14 days ago

3.3.0

17 days ago

3.2.0

29 days ago

3.1.65

1 month ago

3.1.64

2 months ago

3.1.63

2 months ago

3.1.62

2 months ago

3.1.61

2 months ago

3.1.59

2 months ago

3.1.60

2 months ago

3.1.58

2 months ago

3.1.57

3 months ago

3.1.56

3 months ago

3.1.55

3 months ago

3.1.54

3 months ago

3.1.53

3 months ago

3.1.52

3 months ago

3.1.51

3 months ago

3.1.50

4 months ago

3.1.49

4 months ago

3.1.47

4 months ago

3.1.48

4 months ago

3.1.46

5 months ago

3.1.45

5 months ago

3.1.34

9 months ago

3.1.33

9 months ago

3.1.36

9 months ago

3.1.35

9 months ago

3.1.38

8 months ago

3.1.37

8 months ago

3.1.39

6 months ago

3.1.44

5 months ago

3.1.41

6 months ago

3.1.40

6 months ago

3.1.43

5 months ago

3.1.42

6 months ago

3.1.32

11 months ago

3.1.31

12 months ago

3.1.30

1 year ago

3.1.27

1 year ago

3.1.29

1 year ago

3.1.28

1 year ago

3.1.26

1 year ago

3.1.23

1 year ago

3.1.25

1 year ago

3.1.24

1 year ago

3.1.22

1 year ago

3.1.21

1 year ago

3.1.20

1 year ago

3.1.19

1 year ago

3.1.14

1 year ago

3.1.13

2 years ago

3.1.16

1 year ago

3.1.15

1 year ago

3.1.18

1 year ago

3.1.17

1 year ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.0.8

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.7

2 years ago

3.0.4

3 years ago

3.0.6

2 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.1.21

3 years ago

2.1.20

3 years ago

2.1.18

3 years ago

2.1.19

3 years ago

2.1.17

3 years ago

2.1.16

3 years ago

2.1.15

3 years ago

2.1.14

3 years ago

2.1.12

3 years ago

2.1.13

3 years ago

2.1.10

3 years ago

2.1.11

3 years ago

2.1.9

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.4

3 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.19

4 years ago

2.0.18

4 years ago

2.0.17

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.6

5 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago