Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V5 #111

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open

V5 #111

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
dfbb5c5
initial version
planttheidea Aug 26, 2023
1560547
less cloning
planttheidea Aug 26, 2023
6e90b2d
move to double linked list setup for faster changes
planttheidea Aug 27, 2023
0390126
proof that the linked list change speeds up multi-cache usage
planttheidea Aug 27, 2023
acbe0cc
always update the value of existing nodes in `set`, and ensure normal…
planttheidea Aug 27, 2023
217d6e4
consolidate code and add `get` method
planttheidea Aug 27, 2023
790abe6
update dependencies
planttheidea Jan 1, 2024
9525d02
complete migration
planttheidea Jan 1, 2024
de9f11c
fix types for `Cache`
planttheidea Jan 1, 2024
af8f4ad
provide more context with `onCache` events
planttheidea Jan 1, 2024
3df44bf
eliminate private declaration of methods on `Cache` and scope access …
planttheidea Jan 2, 2024
f9a6b21
remove early return for code golf
planttheidea Jan 2, 2024
84409c7
add event emitter for dynamic add / remove of listeners
planttheidea Jan 2, 2024
de52a1b
improve test coverage
planttheidea Jan 2, 2024
930a5bb
update README
planttheidea Jan 2, 2024
2065715
restore `clear` method and playground types, and surface `Cache` / `E…
planttheidea Jan 2, 2024
5dc3c2b
fix default export type
planttheidea Jan 2, 2024
b66aaf7
restore manipulation methods
planttheidea Jan 2, 2024
d513f2f
add tests for mutation methods
planttheidea Jan 3, 2024
7ded0dd
avoid unnecessary update in `.set()`
planttheidea Jan 3, 2024
cc33dce
save a byte on `.set()`
planttheidea Jan 3, 2024
88820cf
split out getting from update for faster operations in specific cases
planttheidea Jan 3, 2024
1781d7d
ensure key is transformed for mutation operations if needed
planttheidea Jan 4, 2024
0f4c855
make event emitter a closure-based POJO and prioritize most recent ca…
planttheidea Jan 4, 2024
eff67a0
simplify notification for events
planttheidea Jan 4, 2024
55a4138
centralize get with transformed key
planttheidea Jan 4, 2024
63b9d80
speed up `cloneKey` and improve test for `transformKey`
planttheidea Jan 5, 2024
33f3a54
use boolean instead of function for more accurate results
planttheidea Jan 5, 2024
0a57c7c
remove getter in favor of standard instance property, to shave bytes
planttheidea Jan 5, 2024
221a077
code golf to shave more bytes
planttheidea Jan 5, 2024
28442d4
rename `snapshot` to `entries` and provide JSDoc for exposed types
planttheidea Jan 5, 2024
f095b80
remove `this.c` since it is not needed
planttheidea Jan 6, 2024
5fb1e56
use named exports to align commonjs with types
planttheidea Jan 6, 2024
b8aa261
update to module repository and improve build types
planttheidea Jan 6, 2024
57bbf3a
remove leftover copy-pasta
planttheidea Jan 6, 2024
78654fc
rename `matchesArg` to `isArgEqual` and `matchesKey` to `isKeyEqual` …
planttheidea Jan 7, 2024
58287c7
update CHANGELOG and README with valid data
planttheidea Jan 8, 2024
30d7263
initial work on next `moize` superset
planttheidea Jan 8, 2024
0ac8e11
Release 5.0.0-beta.0
planttheidea Jan 8, 2024
6612689
CHANGELOG tweaks
planttheidea Jan 8, 2024
061e461
Merge branch 'master' of github.com:planttheidea/micro-memoize into v5
planttheidea Jan 8, 2024
8213ca0
upgrade `mem` and handle it in benchmarks
planttheidea Jan 8, 2024
22486e2
ensure `hit` fires as well as `update` when updating cache
planttheidea Jan 8, 2024
2644509
Release 5.0.0-beta.1
planttheidea Jan 8, 2024
d159f5b
update dependencies to latest
planttheidea Jan 18, 2024
9342a78
fix unit test
planttheidea Jan 18, 2024
17ec572
move pre-release tasks to `release-it` configs
planttheidea Jan 18, 2024
cda4582
Release 5.0.0-beta.2
planttheidea Jan 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.nyc_output
coverage
dist
dist-prev
es
node_modules
lib
Expand Down
6 changes: 0 additions & 6 deletions .prettierrc.js

This file was deleted.

6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "all",
"semi": true,
"singleQuote": true,
"tabWidth": 2
}
13 changes: 9 additions & 4 deletions .release-it.beta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
"github": {
"release": true
},
"hooks": {
"before:init": [
"npm run typecheck",
"npm run lint",
"npm run test:coverage",
"npm run build"
]
},
"npm": {
"tag": "next"
},
"preReleaseId": "beta",
"scripts": {
"beforeStart": "npm run prepublish:compile"
}
"preReleaseId": "beta"
}
9 changes: 7 additions & 2 deletions .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"release": true,
"tagName": "v${version}"
},
"scripts": {
"beforeStart": "npm run prepublish:compile"
"hooks": {
"before:init": [
"npm run typecheck",
"npm run lint",
"npm run test:coverage",
"npm run build"
]
}
}
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# micro-memoize CHANGELOG

## 5.0.0

**Smaller, faster, with more features**

This is a complete rewrite of the library that improves bundle size and runtime performance while also offering more features. If you are using the simple use case of `memoize(fn)` then you should have no impact. However, if you were making use of specific options, you will be impacted.

### Breaking changes

- `isPromise` has been renamed to `async`, to more clearly identify the type of function it is vs what the result of calling it will be
- `isEqual` has been renamed to `isArgEqual`, for better clarity vs `isMatchingKey`
- `isMatchingKey` has been renamed to `isKeyEqual`, for better clarity vs `isEqual`
- `onCacheAdd` / `onCacheChange` / `onCacheHit` has been rearchitected entirely into a dynamic event listener on the `memoized.cache` namespace
- `memoized.cache.snapshot` getter has renamed to `memoized.cache.entries()` method to align with common conventions of `Map`
- Returns an array of `[key, value]` pairs to align with common JS conventions and allow direct population of `Map` / `WeakMap`
- `memoized.options` is now a transparent pass-through of the options provided, instead storing the normalized version

### Enhancements

- Speed improvements
- Reduction in bundle size
- Cache manipulation is easier with added methods:
- `memoized.cache.delete(key)`
- `memoized.cache.get(key)`
- `memoized.cache.has(key)`
- `memoized.cache.set(key, value)`

## 4.1.2

- Republish of [#102](https://github.com/planttheidea/micro-memoize/pull/102)
Expand Down
66 changes: 34 additions & 32 deletions DEV_ONLY/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* globals document */

/* eslint-disable */

import Bluebird from 'bluebird';
import { deepEqual } from 'fast-equals';

import memoize from '../src';
import { Key, RawKey } from '../index.d';

// import '../benchmarks';

Expand Down Expand Up @@ -42,24 +39,31 @@ memoized(bar, foo);
memoized(foo, bar);
memoized(foo, bar);

console.log(memoized.cache.snapshot);
console.log(memoized.cache.entries());
console.log(memoized.cache);

memoized.cache.keys = [];
memoized.cache.values = [];
memoized.cache.clear();

console.log(memoized.cache.snapshot);
console.log(memoized.cache.entries());
console.log(memoized.cache);

console.groupEnd();

console.group('standard with larger cache size');

const memoizedLargerCache = memoize(method, {
onCacheChange(cache) {
console.log([...cache.keys]);
},
maxSize: 3,
const memoizedLargerCache = memoize(memoized, { maxSize: 3 });

memoizedLargerCache.cache.on('add', (event) => {
console.log(event);
});
memoizedLargerCache.cache.on('delete', (event) => {
console.log(event);
});
memoizedLargerCache.cache.on('hit', (event) => {
console.log(event);
});
memoizedLargerCache.cache.on('update', (event) => {
console.log(event);
});

memoizedLargerCache(foo, bar);
Expand All @@ -70,17 +74,15 @@ memoizedLargerCache(foo, bar);
memoizedLargerCache(baz, quz);
memoizedLargerCache(foo, quz);

console.log(memoizedLargerCache.cache.snapshot);
console.log(memoizedLargerCache.cache.entries());

console.groupEnd();

console.group('maxArgs');

// limit to testing the first args
const isMatchingKeyMaxArgs = (originalKey: Key, newKey: RawKey): boolean =>
originalKey[0] === newKey[0];

const memoizedMax = memoize(method, { isMatchingKey: isMatchingKeyMaxArgs });
const memoizedMax = memoize(method, {
isKeyEqual: (originalKey, newKey) => originalKey[0] === newKey[0],
});

memoizedMax(foo, bar);
memoizedMax(foo, baz);
Expand All @@ -103,15 +105,15 @@ const deepEqualMethod = ({
};

const deepEqualMemoized = memoize(deepEqualMethod, {
isEqual: deepEqual,
isArgEqual: deepEqual,
});

deepEqualMemoized({ one: 1, two: 2 });
deepEqualMemoized({ one: 2, two: 1 });
deepEqualMemoized({ one: 1, two: 2 });
deepEqualMemoized({ one: 1, two: 2 });

console.log(deepEqualMemoized.cache.snapshot);
console.log(deepEqualMemoized.cache.entries());

console.groupEnd();

Expand All @@ -128,24 +130,24 @@ const promiseMethod = (number: number, otherNumber: number) => {
const promiseMethodRejected = (number: number) => {
console.log('promise rejection method fired', number);

return new Bluebird((resolve, reject) => {
return new Bluebird((_resolve, reject) => {
setTimeout(() => {
reject(new Error(foo));
}, 100);
});
};

const memoizedPromise = memoize(promiseMethod, { isPromise: true });
const memoizedPromise = memoize(promiseMethod, { async: true });
const memoizedPromiseRejected = memoize(promiseMethodRejected, {
isPromise: true,
async: true,
});

memoizedPromiseRejected(3)
.then((value: any) => {
console.log(value);
})
.catch((error: Error) => {
console.log(memoizedPromiseRejected.cache.snapshot);
console.log(memoizedPromiseRejected.cache.entries());
console.error(error);
});

Expand All @@ -154,7 +156,7 @@ memoizedPromiseRejected(3)
console.log(value);
})
.catch((error: Error) => {
console.log(memoizedPromiseRejected.cache.snapshot);
console.log(memoizedPromiseRejected.cache.entries());
console.error(error);
});

Expand All @@ -163,7 +165,7 @@ memoizedPromiseRejected(3)
console.log(value);
})
.catch((error: Error) => {
console.log(memoizedPromiseRejected.cache.snapshot);
console.log(memoizedPromiseRejected.cache.entries());
console.error(error);
});

Expand All @@ -177,7 +179,7 @@ memoizedPromise(2, 2).then((value: unknown) => {
console.log(`cached value: ${value}`);
});

console.log(memoizedPromise.cache.snapshot.keys);
console.log(memoizedPromise.cache.entries().map(([key]) => key));

console.groupEnd();

Expand Down Expand Up @@ -206,10 +208,10 @@ const noFns = (one: string, two: string, three: Function) => {
};

const memoizedNoFns = memoize(noFns, {
isEqual(key1: string, key2: string) {
isArgEqual(key1, key2) {
return key1 === key2;
},
transformKey(args: any) {
transformKey(args) {
return [JSON.stringify(args)];
},
});
Expand All @@ -231,7 +233,7 @@ const matchingKeyMethod = function (object: {
};

const matchingKeyMemoized = memoize(matchingKeyMethod, {
isMatchingKey: deepEqual,
isKeyEqual: deepEqual,
maxSize: 10,
});

Expand Down Expand Up @@ -299,9 +301,9 @@ const metadata = {
const result1 = calc(data, metadata);

console.log(result1);
console.log(calc.cache.snapshot);
console.log(calc.cache.entries());

const result2 = calc(data, metadata);

console.log(result2);
console.log(calc.cache.snapshot);
console.log(calc.cache.entries());
Loading