-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(best-compare): Fix comparer recursivity (#65)
* Fix comparison recursivity * Prettify
- Loading branch information
Showing
17 changed files
with
113 additions
and
114 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
module.exports = { | ||
"projectName": "perf-best-examples", | ||
projects: [ | ||
'<rootDir>/examples/simple_benchmark', | ||
'<rootDir>/examples/simple_lwc_benchmark', | ||
] | ||
projectName: 'perf-best-examples', | ||
projects: ['<rootDir>/examples/simple_benchmark', '<rootDir>/examples/simple_lwc_benchmark'], | ||
}; |
93 changes: 44 additions & 49 deletions
93
examples/simple_benchmark/src/__benchmarks__/deep_merge.js
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 |
---|---|---|
@@ -1,89 +1,84 @@ | ||
// https://unpkg.com/[email protected]/dist/es.js | ||
|
||
var isMergeableObject = function isMergeableObject(value) { | ||
return isNonNullObject(value) | ||
&& !isSpecial(value) | ||
return isNonNullObject(value) && !isSpecial(value); | ||
}; | ||
|
||
function isNonNullObject(value) { | ||
return !!value && typeof value === 'object' | ||
return !!value && typeof value === 'object'; | ||
} | ||
|
||
function isSpecial(value) { | ||
var stringValue = Object.prototype.toString.call(value); | ||
var stringValue = Object.prototype.toString.call(value); | ||
|
||
return stringValue === '[object RegExp]' | ||
|| stringValue === '[object Date]' | ||
|| isReactElement(value) | ||
return stringValue === '[object RegExp]' || stringValue === '[object Date]' || isReactElement(value); | ||
} | ||
|
||
// see https://github.com/facebook/react/blob/b5ac963fb791d1298e7f396236383bc955f916c1/src/isomorphic/classic/element/ReactElement.js#L21-L25 | ||
var canUseSymbol = typeof Symbol === 'function' && Symbol.for; | ||
var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for('react.element') : 0xeac7; | ||
|
||
function isReactElement(value) { | ||
return value.$$typeof === REACT_ELEMENT_TYPE | ||
return value.$$typeof === REACT_ELEMENT_TYPE; | ||
} | ||
|
||
function emptyTarget(val) { | ||
return Array.isArray(val) ? [] : {} | ||
return Array.isArray(val) ? [] : {}; | ||
} | ||
|
||
function cloneUnlessOtherwiseSpecified(value, optionsArgument) { | ||
var clone = !optionsArgument || optionsArgument.clone !== false; | ||
var clone = !optionsArgument || optionsArgument.clone !== false; | ||
|
||
return (clone && isMergeableObject(value)) | ||
? deepmerge(emptyTarget(value), value, optionsArgument) | ||
: value | ||
return clone && isMergeableObject(value) ? deepmerge(emptyTarget(value), value, optionsArgument) : value; | ||
} | ||
|
||
function defaultArrayMerge(target, source, optionsArgument) { | ||
return target.concat(source).map(function(element) { | ||
return cloneUnlessOtherwiseSpecified(element, optionsArgument) | ||
}) | ||
return target.concat(source).map(function(element) { | ||
return cloneUnlessOtherwiseSpecified(element, optionsArgument); | ||
}); | ||
} | ||
|
||
function mergeObject(target, source, optionsArgument) { | ||
var destination = {}; | ||
if (isMergeableObject(target)) { | ||
Object.keys(target).forEach(function(key) { | ||
destination[key] = cloneUnlessOtherwiseSpecified(target[key], optionsArgument); | ||
}); | ||
} | ||
Object.keys(source).forEach(function(key) { | ||
if (!isMergeableObject(source[key]) || !target[key]) { | ||
destination[key] = cloneUnlessOtherwiseSpecified(source[key], optionsArgument); | ||
} else { | ||
destination[key] = deepmerge(target[key], source[key], optionsArgument); | ||
} | ||
}); | ||
return destination | ||
var destination = {}; | ||
if (isMergeableObject(target)) { | ||
Object.keys(target).forEach(function(key) { | ||
destination[key] = cloneUnlessOtherwiseSpecified(target[key], optionsArgument); | ||
}); | ||
} | ||
Object.keys(source).forEach(function(key) { | ||
if (!isMergeableObject(source[key]) || !target[key]) { | ||
destination[key] = cloneUnlessOtherwiseSpecified(source[key], optionsArgument); | ||
} else { | ||
destination[key] = deepmerge(target[key], source[key], optionsArgument); | ||
} | ||
}); | ||
return destination; | ||
} | ||
|
||
function deepmerge(target, source, optionsArgument) { | ||
var sourceIsArray = Array.isArray(source); | ||
var targetIsArray = Array.isArray(target); | ||
var options = optionsArgument || { arrayMerge: defaultArrayMerge }; | ||
var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray; | ||
|
||
if (!sourceAndTargetTypesMatch) { | ||
return cloneUnlessOtherwiseSpecified(source, optionsArgument) | ||
} else if (sourceIsArray) { | ||
var arrayMerge = options.arrayMerge || defaultArrayMerge; | ||
return arrayMerge(target, source, optionsArgument) | ||
} else { | ||
return mergeObject(target, source, optionsArgument) | ||
} | ||
var sourceIsArray = Array.isArray(source); | ||
var targetIsArray = Array.isArray(target); | ||
var options = optionsArgument || { arrayMerge: defaultArrayMerge }; | ||
var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray; | ||
|
||
if (!sourceAndTargetTypesMatch) { | ||
return cloneUnlessOtherwiseSpecified(source, optionsArgument); | ||
} else if (sourceIsArray) { | ||
var arrayMerge = options.arrayMerge || defaultArrayMerge; | ||
return arrayMerge(target, source, optionsArgument); | ||
} else { | ||
return mergeObject(target, source, optionsArgument); | ||
} | ||
} | ||
|
||
deepmerge.all = function deepmergeAll(array, optionsArgument) { | ||
if (!Array.isArray(array)) { | ||
throw new Error('first argument should be an array') | ||
} | ||
if (!Array.isArray(array)) { | ||
throw new Error('first argument should be an array'); | ||
} | ||
|
||
return array.reduce(function(prev, next) { | ||
return deepmerge(prev, next, optionsArgument) | ||
}, {}) | ||
return array.reduce(function(prev, next) { | ||
return deepmerge(prev, next, optionsArgument); | ||
}, {}); | ||
}; | ||
|
||
var deepmerge_1 = deepmerge; | ||
|
40 changes: 20 additions & 20 deletions
40
examples/simple_benchmark/src/__benchmarks__/mock_data.js
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
export const MOCK_DATA_SMALL = { | ||
"id": "0001", | ||
"type": "donut", | ||
"name": "Cake", | ||
"ppu": 0.55, | ||
"batters": { | ||
"batter":[ | ||
{ "id": "1001", "type": "Regular" }, | ||
{ "id": "1002", "type": "Chocolate" }, | ||
{ "id": "1003", "type": "Blueberry" }, | ||
{ "id": "1004", "type": "Devil's Food" } | ||
] | ||
id: '0001', | ||
type: 'donut', | ||
name: 'Cake', | ||
ppu: 0.55, | ||
batters: { | ||
batter: [ | ||
{ id: '1001', type: 'Regular' }, | ||
{ id: '1002', type: 'Chocolate' }, | ||
{ id: '1003', type: 'Blueberry' }, | ||
{ id: '1004', type: "Devil's Food" }, | ||
], | ||
}, | ||
"topping": [ | ||
{ "id": "5001", "type": "None" }, | ||
{ "id": "5002", "type": "Glazed" }, | ||
{ "id": "5005", "type": "Sugar" }, | ||
{ "id": "5007", "type": "Powdered Sugar" }, | ||
{ "id": "5006", "type": "Chocolate with Sprinkles" }, | ||
{ "id": "5003", "type": "Chocolate" }, | ||
{ "id": "5004", "type": "Maple" } | ||
] | ||
topping: [ | ||
{ id: '5001', type: 'None' }, | ||
{ id: '5002', type: 'Glazed' }, | ||
{ id: '5005', type: 'Sugar' }, | ||
{ id: '5007', type: 'Powdered Sugar' }, | ||
{ id: '5006', type: 'Chocolate with Sprinkles' }, | ||
{ id: '5003', type: 'Chocolate' }, | ||
{ id: '5004', type: 'Maple' }, | ||
], | ||
}; |
4 changes: 2 additions & 2 deletions
4
examples/simple_benchmark/src/__benchmarks__/object_keys.benchmark.js
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
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
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
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
Oops, something went wrong.