https://github.com/shawncplus/memoization-behavior
Polymer behavior adding memoization helpers
https://github.com/shawncplus/memoization-behavior
caching memoize polymer
Last synced: 5 months ago
JSON representation
Polymer behavior adding memoization helpers
- Host: GitHub
- URL: https://github.com/shawncplus/memoization-behavior
- Owner: shawncplus
- License: mit
- Created: 2016-12-21T20:21:09.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-27T18:10:19.000Z (over 9 years ago)
- Last Synced: 2024-08-04T07:02:08.173Z (almost 2 years ago)
- Topics: caching, memoize, polymer
- Language: HTML
- Size: 10.7 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
memoization-behavior [](https://travis-ci.org/shawncplus/memoization-behavior) [](https://beta.webcomponents.org/element/shawncplus/memoization-behavior)
====================
`Biddle.MemoizationBehavior` allows you to define a computed property
as memoizable (http://en.wikipedia.org/wiki/Memoization) with `_memoizeComputed`
or more generally memoize any given function with `_memoize`
> Note: Methods of this behavior are "private" as a matter of convention and will
> not appear on beta.webcomponents.org hosted documentation. I've made the
> executive decision that it wouldn't be wise to allow the memoization methods
> to be published as part of an element's public API. Doing so may entice some
> hapless individual into creating some kind of element whose purpose is to
> memoize all computed properties in all elements ever.
To implement a computed property as memoizable in the simplest fashion (although
this is a trivial example, you would likely only want to memoize expensive functions)
behaviors: [
Biddle.MemoizationBehavior,
],
properties: {
firstName: String,
lastName: String,
fullName: {
type: String,
computed: '_memoizeComputed("fullName", firstName, lastName)'
method: '_getFullName'
}
},
_getFullName: function (first, last) {
return first + ' ' + last;
}
Given that the default hash function is `JSON.stringify` which may be inappropriate
for object/array parameters you may wish to override the default hashing method
for a specific property.
properties: {
foo: Object,
bar: Object,
foobar: {
type: String,
computed: '_memoizeComputed("fullName", foo, bar)'
method: '_computeFoobar',
hashFn: (foo, bar) => foo.id + ':' + bar.id
}
},
To generally memoize any function you may do the following
var someValue = this._memoize(this.someExpensiveFunction)(arg2, arg2, arg3);
Cache invalidation happens manually with the `_invalidateMemoizeCache` method
var foo = this._memoize(this.bar)('Hello');
// ...
this._invalidateMemoizeCache(this.bar);