This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Perf optimizations #27
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const memoize = function (fn) { | ||
const cache = {}; | ||
return function () { | ||
const args = Array.prototype.slice.call(arguments); | ||
const hash = args.map((arg) => { | ||
return (typeof arg === "string" || typeof arg === "number") ? arg : JSON.stringify(arg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good thought. |
||
}).join("~"); | ||
return hash in cache ? | ||
cache[hash] : | ||
cache[hash] = fn.apply(this, args); // eslint-disable-line no-invalid-this | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you paste a code comment with a reference / links to where we're getting our memoize function from? There are a ton of battle-tested examples out there, and hopefully we can leverage a good, well-trodden path here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to make a change here if you think it is necessary, but let me explain my thinking. If you look at the
memoize
function that is included with Lodash, there isn't much to it. If aresolver
is included, all arguments will be passed (this is not the case with Underscore'smemoize
), which makes usingLodash#memoize
a possibility.However, the
resolver
key-generation will consist of basically all of the code in this PR'smemoize
, plus overhead of an additionalfn.apply
. Since the motivation for this PR is to improve performance, and because thismemoize
is in the hot code path, I wanted to reduce any unnecessary overhead.In other words, I'm fine changing this to
_.memoize(fn, /* all the code in perf.js */)
, but it doesn't seem to buy us anything and it will mean a slight perf hit.