Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Custom Merge raf
(version: 0)
Comparing performance of:
them vs raf vs me
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function isObject(item) { return item && typeof item === 'object' && !Array.isArray(item) } function themeMerge(target, source) { let output = Object.assign({}, target) if (isObject(target) && isObject(source)) { Object.keys(source).forEach(key => { if (isObject(source[key])) { if (!(key in target)) Object.assign(output, { [key]: source[key] }) else output[key] = themeMerge(target[key], source[key]) } else { Object.assign(output, { [key]: source[key] }) } }) } return output } function emptyTarget(val) { return Array.isArray(val) ? [] : {} } function cloneUnlessOtherwiseSpecified(value, options) { return (options.clone !== false && options.isObject(value)) ? deepmerge(emptyTarget(value), value, options) : value } function defaultArrayMerge(target, source, options) { return target.concat(source).map(function(element) { return cloneUnlessOtherwiseSpecified(element, options) }) } function getMergeFunction(key, options) { if (!options.customMerge) { return deepmerge } var customMerge = options.customMerge(key) return typeof customMerge === 'function' ? customMerge : deepmerge } function mergeObject(target, source, options) { var destination = {} if (options.isObject(target)) { Object.keys(target).forEach(function(key) { destination[key] = cloneUnlessOtherwiseSpecified(target[key], options) }) } Object.keys(source).forEach(function(key) { if (!options.isObject(source[key]) || !target[key]) { destination[key] = cloneUnlessOtherwiseSpecified(source[key], options) } else { destination[key] = getMergeFunction(key, options)(target[key], source[key], options) } }) return destination } function deepmerge(target, source, options) { options = options || {} options.arrayMerge = options.arrayMerge options.isObject = options.isObject var sourceIsArray = Array.isArray(source) var targetIsArray = Array.isArray(target) var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray if (!sourceAndTargetTypesMatch) { return cloneUnlessOtherwiseSpecified(source, options) } else if (sourceIsArray) { return options.arrayMerge(target, source, options) } else { return mergeObject(target, source, options) } }
Tests:
them
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = _.merge(a, b);
raf
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = merge(a, b);
me
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = themeMerge(a, b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
them
raf
me
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided Benchmark Definition JSON and explain what is tested, compared, and their pros and cons. **Benchmark Definition** The benchmark defines three functions: `themeMerge`, `mergeObject`, and `deepmerge`. These functions are used for merging two objects or arrays. The main difference between them lies in how they handle different data types, such as arrays and non-object values. * **`themeMerge`**: This function is a custom implementation of object merge, which combines the features of both `mergeObject` and `deepmerge`. It first merges all top-level properties from both objects using `Object.assign()` and then recursively merges nested objects. * **`mergeObject`**: This function takes an optional `options` object to customize its behavior. It returns a new object with merged properties from the source and target objects. If an option is provided, it uses `cloneUnlessOtherwiseSpecified()` to clone non-object values before merging them. * **`deepmerge`**: This is another implementation of object merge, which uses recursive merging for nested objects. It takes additional options like `arrayMerge` and `isObject` to customize its behavior. **Comparison** The benchmark compares the execution performance of these three functions: 1. `themeMerge` 2. `mergeObject` 3. `deepmerge` Each function is tested with a different set of test cases, which are defined in the "Individual test cases" section. * **`them`**: This test case uses `_.merge(a, b)`, where `a` and `b` are two objects to be merged. * **`raf`**: This test case uses `merge(a, b)`, where `a` and `b` are two objects to be merged. Note that the function name is slightly different from `themeMerge`. * **`me`**: This test case uses `themeMerge(a, b)`, where `a` and `b` are two objects to be merged. **Pros and Cons** Here's a brief analysis of each function: 1. **`themeMerge`**: It combines the strengths of both `mergeObject` and `deepmerge`. However, its implementation might be slightly slower due to using `Object.assign()`. * Pros: Easy to implement and maintain, handles nested objects recursively. * Cons: Might be slower than other implementations. 2. **`mergeObject`**: This function provides flexibility through the use of custom options. However, its performance can degrade if non-object values are involved in merging. * Pros: Customizable, can handle various data types during merging. * Cons: Performance might suffer when dealing with complex objects and non-object values. 3. **`deepmerge`**: This implementation is efficient for large object graphs but requires additional options to customize its behavior. * Pros: Fast execution, handles nested objects efficiently. * Cons: Might be less intuitive for developers without prior experience with recursive merging. **Latest Benchmark Result** The latest benchmark result shows the execution performance of each function over multiple executions per second. The results indicate that: 1. **`themeMerge`**: Provides a balance between ease of implementation and performance, but its speed is slightly lower than `deepmerge`. 2. **`mergeObject`**: Exhibits slower performance due to handling non-object values during merging. 3. **`deepmerge`**: Offers the fastest execution time among the three functions. Keep in mind that these results might vary depending on the specific use case, input data, and system configuration.
Related benchmarks:
Array Properties Merge: Lodash merge vs Object.assign
array shallow clone comparison narrowed
array of objects shallow clone comparison narrowed
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?