Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comparing different object merge methods
(version: 0)
Comparing performance of:
Lodash merge vs object.assign vs spread vs spread mutate
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>
Tests:
Lodash merge
var a = { a: 'foo', b: 'bar' }; var b = { c: 'foobar' }; var c = _.merge(a, b);
object.assign
var a = { a: 'foo', b: 'bar' }; var b = { c: 'foobar' }; var c = Object.assign(a, b);
spread
var a = { a: 'foo', b: 'bar' }; var b = { c: 'foobar' }; var c = { ...a, ...b };
spread mutate
var a = { a: 'foo', b: 'bar' }; var b = { c: 'foobar' }; a = { ...a, ...b };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash merge
object.assign
spread
spread mutate
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 details of what is being tested in this benchmark. The provided JSON represents a JavaScript microbenchmark that compares four different approaches for merging objects: 1. **Lodash merge (`_.merge(a, b)`)**: This method is part of the popular Lodash library and provides a flexible way to merge two or more objects. It recursively merges properties from source (the first object) into target (the second object). Pros: * Flexible and powerful * Supports merging multiple sources Cons: * Adds unnecessary overhead due to its complexity * May not be suitable for simple object merges 2. **`Object.assign()`**: This method is a built-in JavaScript function that returns a new object with the properties of one or more source objects. Pros: * Lightweight and efficient * Built-in, so no extra library needed Cons: * Only works on two objects; merging multiple sources can be cumbersome 3. **Spread operator (`{ ...a, ...b }`)**: This syntax was introduced in ECMAScript 2018 (ES2020) and provides a concise way to merge two or more objects. Pros: * Concise and readable * Efficient, as it avoids the need for an explicit loop Cons: * Only works in modern browsers that support ES2020+ * May not be compatible with older JavaScript environments 4. **Spread operator mutation (`a = { ...a, ...b }`)**: This syntax is similar to the previous one but modifies the original object instead of creating a new one. Pros: * Suitable for cases where the original object needs to be modified * Can be faster than creating a new object Cons: * Modifies the original object, which may not be desirable in some situations * Only works in modern browsers that support ES2020+ The benchmark measures the execution time of each approach on a simple object merge scenario. The `Html Preparation Code` includes the Lodash library to ensure that all test cases use the same environment. In terms of special JavaScript features or syntax, this benchmark uses the spread operator (introduced in ECMAScript 2018) and object mutation with spread operator (`a = { ...a, ...b }`). These are relatively new features and may not be supported by older browsers or environments. Other alternatives to these approaches include: * Using a library like MergeStream, which provides a fast and efficient way to merge streams of data * Implementing a custom merge function using a loop or recursion * Using a different object merging algorithm, such as the "concat" approach However, for most use cases, the four approaches mentioned above (Lodash merge, `Object.assign()`, spread operator, and spread operator mutation) are sufficient and efficient.
Related benchmarks:
Object.assign vs Lodash.merge
lodash merge vs object.assign vs spread 3
Array Properties Merge: Lodash merge vs Object.assign
lodash assign vs object.assign vs spread operator - variable and constant
Lodash merge vs mergedeep 1
Comments
Confirm delete:
Do you really want to delete benchmark?