Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash assign vs object.assign vs spread lodash 4.17.10
(version: 0)
Comparing performance of:
lodash assign vs object.assign vs spread
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.10/lodash.min.js'></script>
Tests:
lodash assign
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = _.assign({}, a, b);
object.assign
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = Object.assign({}, a, b);
spread
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = { ...a, ...b };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash assign
object.assign
spread
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 world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark compares three ways to merge objects in JavaScript: using `_.assign()` from Lodash, `Object.assign()`, and the spread operator (`{ ...a, ...b }`). The test cases are identical, with two source objects (`a` and `b`) being merged into a third object (`c`). **Options Compared** 1. **Lodash _.assign()**: This method takes an arbitrary number of arguments and merges them into a new object. 2. **Object.assign()**: This method is used to copy the values of all enumerable own properties from one or more source objects to a destination object. **Pros and Cons of Each Approach** ### Lodash _.assign() Pros: * More flexible, as it can take any number of arguments * Often preferred due to its concise syntax Cons: * Adds an extra dependency (Lodash) for the benchmark * Can lead to slower performance if not optimized ### Object.assign() Pros: * Built-in method, so no additional dependencies are required * Fast and efficient Cons: * Limited flexibility, as it can only take one source object at a time * Less concise syntax compared to Lodash _.assign() ### Spread Operator ({ ...a, ...b }) Pros: * Concise and expressive syntax * Fast and efficient, as it creates a new object using shallow copying Cons: * Only available in modern JavaScript engines ( ECMAScript 2018+ ) * Can lead to unexpected behavior if not used carefully **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks such as array manipulation, object merging, and more. The `_` symbol is often used as an alias for the global `lodash` variable. **Special JS Feature/Syntax: Spread Operator (ECMAScript 2018+)** The spread operator (`{ ...a, ...b }`) is a new syntax introduced in ECMAScript 2018+. It allows you to create a new object by spreading the properties of two or more source objects. This feature has improved performance and readability compared to traditional object merging methods. **Benchmark Results** According to the latest benchmark results, Chrome 69 on Linux provides the fastest execution speed for all three options: 1. **Lodash _.assign()**: ~15.3 ms per execution 2. **Object.assign()**: ~10.5 ms per execution 3. **Spread Operator ({ ...a, ...b })**: ~8.4 ms per execution Keep in mind that these results may vary depending on the specific benchmarking environment and test cases. **Other Alternatives** If you're interested in exploring alternative object merging methods, consider: 1. **Object.create()**: A more verbose way to create a new object by cloning an existing one. 2. **Object.assign( destination, ...sources )**: A shorthand version of the spread operator for assigning values to an existing object. However, these alternatives may not be as concise or efficient as the options tested in this benchmark.
Related benchmarks:
lodash.assign vs object.assign vs spread
lodash assign vs spread
lodash assign vs object.assign vs spread operator - variable and constant
Object.assign vs Lodash.assign (4.17.21)
Lodash.assign vs Object.assign vs spread assign
Comments
Confirm delete:
Do you really want to delete benchmark?