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

Quality of life improvements #124

Merged
merged 67 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
5c461df
initial
alexreardon Aug 31, 2021
be62c91
typing goodness
alexreardon Aug 31, 2021
90120e0
trying weakmaps
alexreardon Aug 31, 2021
90a96b3
using map for now
alexreardon Aug 31, 2021
d0e81d7
making name writing because why not
alexreardon Aug 31, 2021
0012074
adding minor tests
alexreardon Aug 31, 2021
df7a8d5
minor reformatting
alexreardon Aug 31, 2021
4a50848
better naming for anonymous fns
alexreardon Aug 31, 2021
37c2f14
using a variable rather than a map for now to hold cache
alexreardon Aug 31, 2021
a14f72b
removing redundant property
alexreardon Sep 1, 2021
c925f41
more types
alexreardon Sep 1, 2021
a24ae2f
continue to improve types
alexreardon Sep 1, 2021
ef02130
adding type improvements and tests
alexreardon Sep 1, 2021
240c487
minor type move
alexreardon Sep 1, 2021
4544976
comment update
alexreardon Sep 1, 2021
d9f4df0
test for type of clear function
alexreardon Sep 1, 2021
f9feb89
adding test for cache clearing
alexreardon Sep 2, 2021
9c79669
shifting over to main, removing fn naming
alexreardon Sep 26, 2021
9f3d201
fixing test links
alexreardon Sep 26, 2021
60d9251
renaming test file, adding comments
alexreardon Sep 26, 2021
2846fd6
updating a test name
alexreardon Sep 26, 2021
df61a4d
bumping deps
alexreardon Sep 26, 2021
4f95b68
bumping deps, fixing 3rd party types
alexreardon Sep 26, 2021
43dd445
baselining library comparison
alexreardon Sep 26, 2021
c4452d3
adding lodash.memoize
alexreardon Sep 26, 2021
6be9f57
perf runner working
alexreardon Sep 26, 2021
df68217
disabling ts
alexreardon Sep 26, 2021
e82c14a
wip
alexreardon Sep 26, 2021
b615c77
leaning into ts
alexreardon Sep 27, 2021
7adb481
removing old files
alexreardon Sep 27, 2021
a9a8f1e
nice output
alexreardon Sep 27, 2021
1792ea7
adding my libraries
alexreardon Sep 27, 2021
3ee2ee9
heavier slow fn
alexreardon Sep 27, 2021
07c60e2
removing travis
alexreardon Sep 27, 2021
9b2cb7d
adding github workflows
alexreardon Sep 27, 2021
2a2edcf
fixing eslint
alexreardon Sep 28, 2021
16844f2
fixing commands
alexreardon Sep 28, 2021
21be245
updating comment
alexreardon Sep 28, 2021
7d74046
removing unused dependabot ignore
alexreardon Sep 28, 2021
82cf3af
type test for equalityfn
alexreardon Oct 1, 2021
a26cb54
removing whitespace
alexreardon Oct 1, 2021
35231c9
tweaking the readme
alexreardon Oct 3, 2021
ae6d858
adding performance results
alexreardon Oct 6, 2021
806a028
adding more to readme
alexreardon Oct 11, 2021
752bc49
adding section about function properties to readme
alexreardon Oct 11, 2021
664007e
adding notes
alexreardon Oct 11, 2021
dfcf27b
minor disclaimer
alexreardon Oct 11, 2021
e2f12d3
wip tests
alexreardon Oct 11, 2021
02911d8
fleshing out test
alexreardon Oct 11, 2021
f239f05
adding equality types to readme
alexreardon Oct 11, 2021
2f98c12
adding docs about .clear()
alexreardon Oct 11, 2021
73f6d9b
removing comment
alexreardon Oct 15, 2021
2a2c84b
v6.0.0-beta.1
alexreardon Oct 15, 2021
84f2b21
sneaky flowtypes
alexreardon Oct 15, 2021
8b12bfb
exporting MemoizedFn type
alexreardon Oct 18, 2021
ae7a643
adding MemoizeFn generic
alexreardon Oct 18, 2021
dd27948
minor tweaks
alexreardon Oct 18, 2021
19b26b4
improving language in docs
alexreardon Oct 19, 2021
4872312
bumping node version (again)
alexreardon Oct 19, 2021
3e75de0
moving to node 16
alexreardon Oct 19, 2021
ce0c6bf
outputting results to markdown tables
alexreardon Oct 19, 2021
4c5409b
formatting results
alexreardon Oct 19, 2021
18b689f
updating results
alexreardon Oct 19, 2021
cd46d47
removing temp type:module
alexreardon Oct 19, 2021
994ef04
updating perf benchmark as lodash only uses the first argument as the…
alexreardon Oct 19, 2021
d8246db
updating copy in readme
alexreardon Oct 20, 2021
e5a4d8b
adding tests for casting the type of a memoized function
alexreardon Oct 20, 2021
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
Prev Previous commit
Next Next commit
shifting over to main, removing fn naming
  • Loading branch information
alexreardon committed Sep 26, 2021
commit 9c79669a52ed1a47b3a0647ead335aed63f255ed
66 changes: 41 additions & 25 deletions src/memoize-one.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
import areInputsEqual from './are-inputs-equal';

// Using ReadonlyArray<T> rather than readonly T as it works with TS v3
export type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;

function memoizeOne<
// Need to use 'any' rather than 'unknown' here as it has
// The correct Generic narrowing behaviour.
ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>
>(resultFn: ResultFn, isEqual: EqualityFn = areInputsEqual): ResultFn {
let lastThis: unknown;
let lastArgs: unknown[] = [];
let lastResult: ReturnType<ResultFn>;
let calledOnce: boolean = false;
export type EqualityFn<TFunc extends (...args: any[]) => any> = (
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types of the EqualityFn are now based on the function that it is being used for

newArgs: Parameters<TFunc>,
lastArgs: Parameters<TFunc>,
) => boolean;

type Cache<TFunc extends (this: any, ...args: any[]) => any> = {
lastThis: ThisParameterType<TFunc>;
lastArgs: Parameters<TFunc>;
lastResult: ReturnType<TFunc>;
};

type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we were casting the return value to be ResultFn

return memoized as ResultFn;

That is technically not correct as we are not maintaining any properties on the function itself. We now return a function that has the same type as the functions call signature and we are explicitly adding a .clear property to that

clear: () => void;
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>;
};

function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>(
resultFn: TFunc,
isEqual: EqualityFn<TFunc> = areInputsEqual,
): MemoizedFn<TFunc> {
let cache: Cache<TFunc> | null = null;

// breaking cache when context (this) or arguments change
function memoized(this: unknown, ...newArgs: unknown[]): ReturnType<ResultFn> {
if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) {
return lastResult;
function memoized(
this: ThisParameterType<TFunc>,
...newArgs: Parameters<TFunc>
): ReturnType<TFunc> {
if (cache && cache.lastThis === this && isEqual(newArgs, cache.lastArgs)) {
return cache.lastResult;
}

// Throwing during an assignment aborts the assignment: https://codepen.io/alexreardon/pen/RYKoaz
// Doing the lastResult assignment first so that if it throws
// nothing will be overwritten
lastResult = resultFn.apply(this, newArgs);
calledOnce = true;
lastThis = this;
lastArgs = newArgs;
// the cache will be overwritten
const lastResult = resultFn.apply(this, newArgs);
cache = {
lastResult,
lastArgs: newArgs,
lastThis: this,
};

return lastResult;
}

return memoized as ResultFn;
// Adding the ability to clear the cache of a memoized function
memoized.clear = function clear() {
cache = null;
};

return memoized;
}

// default export
export default memoizeOne;

// disabled for now as mixing named and
// default exports is problematic with CommonJS
// export { memoizeOne };
66 changes: 0 additions & 66 deletions src/next.ts

This file was deleted.