Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
asjhkf
(version: 0)
Comparing performance of:
them vs raf vs James Gill
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 = deepmerge(a, b);
James Gill
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
James Gill
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):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is a set of JavaScript functions that are used to merge two objects, `target` and `source`, into a single object. The functions are: 1. `themeMerge`: This function uses the `Object.assign()` method to create a copy of the `target` object and then iterates through the keys in the `source` object. If the key is an object itself, it recursively merges the two objects using `themeMerge`. Otherwise, it simply assigns the value from the `source` object to the `output` object. 2. `deepmerge`: This function checks if both `target` and `source` are arrays or objects. If they are not, it simply returns the original value. If they are arrays, it uses the `arrayMerge()` method (not shown in this definition) to merge the two arrays. If they are objects, it recursively calls itself for each key that needs to be merged. 3. `cloneUnlessOtherwiseSpecified`: This function checks if an option is present (`clone` and `isObject`) and returns a cloned version of the value using `deepmerge()` or simply returns the original value. **Options** The options object can have several properties: * `arrayMerge`: This property specifies how to merge two arrays. * `customMerge`: This property allows for custom merging functions to be specified for specific keys. * `isObject`: This property indicates whether the `target` and `source` values are objects or not. **Pros and Cons** The pros of using these merging functions are: * `themeMerge` is efficient for small to medium-sized objects since it uses `Object.assign()` under the hood, which is a very fast operation. * `deepmerge` provides more flexibility for merging complex objects with nested structures. * `cloneUnlessOtherwiseSpecified` ensures that values are cloned only when necessary, reducing unnecessary copies. However, there are some cons: * The `themeMerge` function has a recursive depth limit (not shown in this definition), which may cause issues if the object structure is too deep. * The `deepmerge` function can be slower than `themeMerge` for very large objects since it recursively merges all properties. **Library** The `lodash` library is used in the benchmark, specifically the `_` symbol refers to the Lodash.js namespace. It provides several useful functions, including `Object.assign()`, which is used in `themeMerge`. **Special JS feature/syntax** There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. **Other alternatives** Some other alternatives for merging objects and arrays include: * The native `Array.prototype.concat()` method can be used to merge two arrays, but it does not handle nested structures. * A custom implementation using a simple loop to iterate through the keys in both objects would also work, but would likely be slower than the provided implementations. Overall, this benchmark compares the performance of three different merging functions for merging objects and arrays: `themeMerge`, `deepmerge`, and `cloneUnlessOtherwiseSpecified`.
Related benchmarks:
Lodash Custom Merge
Lodash Custom Merge raf
Lodash vs Object.assign
test for Vovan
Comments
Confirm delete:
Do you really want to delete benchmark?