-
Notifications
You must be signed in to change notification settings - Fork 94
Use faster implementation for merging #186
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
Changes from all commits
62e7174
5697177
d0a9642
a45cc7b
da670b6
41961d7
f57cacb
d0351ab
c1b1df4
6decdc2
6415da0
494c152
3e6cf77
3ba5681
44db3fc
0cf356f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Mostly copied from https://medium.com/@lubaka.a/how-to-remove-lodash-performance-improvement-b306669ad0e1 | ||
|
|
||
| /** | ||
| * @param {mixed} val | ||
| * @returns {boolean} | ||
| */ | ||
| function isMergeableObject(val) { | ||
luacmartins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const nonNullObject = val != null ? typeof val === 'object' : false; | ||
| return (nonNullObject | ||
| && Object.prototype.toString.call(val) !== '[object RegExp]' | ||
| && Object.prototype.toString.call(val) !== '[object Date]'); | ||
| } | ||
|
|
||
| /** | ||
| * @param {Object} target | ||
| * @param {Object} source | ||
| * @returns {Object} | ||
| */ | ||
| function mergeObject(target, source) { | ||
| const destination = {}; | ||
| if (isMergeableObject(target)) { | ||
| // lodash adds a small overhead so we don't use it here | ||
| // eslint-disable-next-line rulesdir/prefer-underscore-method | ||
| const targetKeys = Object.keys(target); | ||
| for (let i = 0; i < targetKeys.length; ++i) { | ||
| const key = targetKeys[i]; | ||
| destination[key] = target[key]; | ||
| } | ||
| } | ||
|
|
||
| // lodash adds a small overhead so we don't use it here | ||
| // eslint-disable-next-line rulesdir/prefer-underscore-method | ||
|
Collaborator
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. Is there any reason not to use underscore here? If it's because of performance, please add a code comment explaining that.
Contributor
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. +1
Contributor
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. Why does the function has to be defined as
Contributor
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. We have to do that otherwise we would use it before it's defined, since we have the following circular dependency
Collaborator
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. +1
Contributor
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. when 2 functions are on the same level, they are registered at the same time, it's just that eslint would flag it as a code style issue, because our code style is to define stuff before it's used - impossible to address in case of a circular dependency, but we can just disable the warning The actual problem is with variables - you can't use a variable before it is defined, but you can use a function before it's defined, because functions are always registered before any code runs
Contributor
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. Thanks for clarifying @kidroca. In that case, I agree that we should declare it as |
||
| const sourceKeys = Object.keys(source); | ||
| for (let i = 0; i < sourceKeys.length; ++i) { | ||
| const key = sourceKeys[i]; | ||
| if (source[key] === undefined) { | ||
| // eslint-disable-next-line no-continue | ||
| continue; | ||
| } | ||
| if (!isMergeableObject(source[key]) || !target[key]) { | ||
| destination[key] = source[key]; | ||
| } else { | ||
| // eslint-disable-next-line no-use-before-define | ||
| destination[key] = fastMerge(target[key], source[key]); | ||
luacmartins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return destination; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Object|Array} target | ||
| * @param {Object|Array} source | ||
| * @returns {Object|Array} | ||
| */ | ||
| function fastMerge(target, source) { | ||
| // lodash adds a small overhead so we don't use it here | ||
| // eslint-disable-next-line rulesdir/prefer-underscore-method | ||
| const array = Array.isArray(source); | ||
| if (array) { | ||
| return source; | ||
| } | ||
| return mergeObject(target, source); | ||
| } | ||
|
|
||
| export default fastMerge; | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.