Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
helpers.merge vs spread vs underscore.extend
(version: 0)
Comparing performance of:
helpers.merge vs object.assign vs underscore.extend
Created:
5 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>
Tests:
helpers.merge
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var merge = function (a, b, options) { if (options == null) { options = {}; } if (!a) { return b; } if (!b) { return a; } var deep = options.deep || false; var sum = options.sum || false; var r ={}; for(var i in a ){ r[i] = a[i]; } for(var j in b ){ if (deep && r.hasOwnProperty(j) && this.isObject(r[j]) && this.isObject(b[j])) { r[j] = this.merge(r[j], b[j], options); } else { if( sum === true ) { if( ( jpath.isNumber( r[ j ] ) === true ) && ( jpath.isNumber( b[ j ] ) === true ) ) { r[j] += b[j]; } else { r[j] = b[j]; } } else { r[j] = b[j]; } } } return r; }; var c = merge(a, b);
object.assign
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = { ...a, ...b}
underscore.extend
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var createAssigner = function(keysFunc, undefinedOnly) { return function(obj) { var length = arguments.length; if (length < 2 || obj == null) return obj; for (var index = 1; index < length; index++) { var source = arguments[index], keys = keysFunc(source), l = keys.length; for (var i = 0; i < l; i++) { var key = keys[i]; if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key]; } } return obj; }; }; var allKeys = function(obj) { if (!_.isObject(obj)) return []; var keys = []; for (var key in obj) keys.push(key); return keys; }; var extend = createAssigner(allKeys); var c = extend(a, b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
helpers.merge
object.assign
underscore.extend
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):
Measuring the performance of JavaScript merge functions is an interesting benchmark! **Overview** The provided JSON represents a microbenchmarking test suite, where three different approaches are compared: 1. `helpers.merge` (a custom implementation) 2. `object.assign` (a built-in JavaScript method) 3. `underscore.extend` (a library function) Each approach has its pros and cons. **`helpers.merge`** This is the custom implementation provided in the benchmark definition. It's a recursive function that takes two objects, `a` and `b`, as input and returns their merged result. The function also accepts optional `options` object with two properties: `deep` and `sum`. Pros: * Customizable behavior through the `options` object * Supports merging nested objects Cons: * More complex implementation compared to built-in methods * May be slower due to recursive calls **`object.assign`** This is a built-in JavaScript method that returns a new object created by copying all enumerable own properties from one or more source objects. Pros: * Fast and lightweight implementation * Built-in, so it's likely optimized for performance Cons: * Limited customization options * Only merges top-level properties, not nested ones **`underscore.extend`** This is a library function provided by the Lodash utility library. It creates a new object with the properties of two source objects. Pros: * Fast and lightweight implementation * Supports merging nested objects through optional `deep` parameter Cons: * External dependency on the Lodash library * May be slower than built-in methods due to function call overhead **Other considerations** When choosing between these approaches, consider the following factors: * Performance: If speed is critical, `object.assign` might be the best choice. For more complex merging scenarios or customization, `helpers.merge` might be a better fit. * Customization: If you need fine-grained control over the merging process, `helpers.merge` offers the most flexibility. * Library dependencies: If using Lodash library is acceptable, `underscore.extend` can provide a convenient and efficient solution. **Alternatives** If none of these approaches suit your needs, consider other merge functions or libraries: 1. `Array.prototype.reduce()`: Can be used for merging objects by reducing an array of property-value pairs. 2. `JSON.merge()`: An older ECMAScript method for merging JSON objects (not supported in modern browsers). 3. Other library functions, such as `merge` from the `deep-extend` or `extend` from the `fast-deep-equal` libraries. Keep in mind that these alternatives might have varying degrees of complexity and performance characteristics compared to the original benchmark implementations.
Related benchmarks:
lodash merge vs deepmerge vs lodash/fp merge
lodash merge vs object.assign vs spread (v2)
lodash merge vs object.assign vs spread (v3)
lodash merge vs deepmerge 4.2.2 vs own merge implementation
Lodash merge vs mergedeep 1
Comments
Confirm delete:
Do you really want to delete benchmark?