Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
helpers.merge vs object.assign vs underscore.extend
(version: 0)
Comparing performance of:
helpers.merge vs object.assign vs underscore.extend
Created:
7 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 = Object.assign(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):
Let's dive into the provided benchmark definition and test cases. **Overview** The benchmark measures the performance of three different approaches to merge two objects: 1. `helpers.merge`: A custom merge function implemented by the user. 2. `object.assign`: The built-in `Object.assign()` method in JavaScript. 3. `underscore.extend`: The `extend` function from the Lodash library. **Options Compared** In each test case, the options are compared to determine how they affect the performance of the merge operation. Specifically: * `sum`: A boolean flag that determines whether to sum up numerical values or overwrite them with new values. * `deep`: A boolean flag that determines whether to recursively merge nested objects. **Pros and Cons** Here's a brief overview of each approach: 1. **`helpers.merge`**: * Pros: Customizable, flexible, and allows for advanced merge logic using the provided function. * Cons: More complex, error-prone, and may have performance implications due to function call overhead. 2. **`object.assign()`**: * Pros: Simple, efficient, and widely supported. * Cons: Limited control over the merge operation, as it only supports shallow assignment. 3. **`underscore.extend()`**: * Pros: Flexible, efficient, and provides a simple way to extend objects with new properties. * Cons: Requires an additional library (Lodash), which may not be desirable for all users. **Library: Lodash** The `underscore.extend()` function is part of the Lodash library, which provides a comprehensive set of utility functions for JavaScript development. In this benchmark, the `extend` function is used to create an assigner that can merge objects with a specific set of keys and options. **Special JS Feature or Syntax** There is no special JS feature or syntax used in these benchmarks. However, it's worth noting that the use of Lodash requires users to include an additional library in their codebase, which may not be desirable for all projects. **Other Alternatives** If you need a custom merge function similar to `helpers.merge`, you can consider using: 1. **`JSON.parse(JSON.stringify(obj))`**: This approach can be used to create a deep copy of an object and then recursively merge the copied objects. 2. **`Object.create(null)` + `Object.assign()`**: This approach uses `Object.create(null)` to create a new object with no prototype, and then uses `Object.assign()` to merge properties from two objects. These alternatives may not offer the same level of flexibility as `helpers.merge`, but they can provide a simpler solution for certain use cases.
Related benchmarks:
lodash merge vs object.assign vs spread new obj
lodash.assign vs object.assign vs spread
Array Properties Merge: Lodash merge vs Object.assign
lodash merge vs object.assign vs spread (no intermediate vars)
lodash assign vs object.assign vs spread operator - variable and constant
Comments
Confirm delete:
Do you really want to delete benchmark?