<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var a = { "_id": "624863161dde7aa8538857e6",
"index": 0,
"guid": "4be322b0-459c-41aa-bd09-f3227cd353df",
"isActive": false,
"balance": "$3,816.12",
"picture": "http://placehold.it/32x32",
"age": 26,
"eyeColor": "brown",
"name": "Caitlin Beach",
"gender": "female",
"company": "MICROLUXE",
"email": "caitlinbeach@microluxe.com",
"phone": "+1 (954) 462-3548",
"address": "854 Sapphire Street, Biddle, Colorado, 1002",
"about": "Adipisicing proident labore qui est minim tempor. Ipsum est cillum nisi exercitation laboris irure. Enim tempor elit quis est deserunt cupidatat dolore. Mollit aliquip enim proident non commodo non sunt tempor nisi elit. Deserunt ex proident mollit esse. Ea Lorem sit ipsum occaecat ad ullamco.\r\n",
"registered": "2018-03-22T05:20:17 +07:00",
"latitude": 69.438455,
"longitude": 113.552142 };
var b = { "eyeColor": "bdrown",
"name": "Caitlind Beach",
"gender": "femalde",
"company": "MICRdOLUXE",
"email": "caitlindbeach@microluxe.com",
"phone": "+1 (954)d 462-3548", };
var c = _.merge(a, b);
var a = { "_id": "624863161dde7aa8538857e6",
"index": 0,
"guid": "4be322b0-459c-41aa-bd09-f3227cd353df",
"isActive": false,
"balance": "$3,816.12",
"picture": "http://placehold.it/32x32",
"age": 26,
"eyeColor": "brown",
"name": "Caitlin Beach",
"gender": "female",
"company": "MICROLUXE",
"email": "caitlinbeach@microluxe.com",
"phone": "+1 (954) 462-3548",
"address": "854 Sapphire Street, Biddle, Colorado, 1002",
"about": "Adipisicing proident labore qui est minim tempor. Ipsum est cillum nisi exercitation laboris irure. Enim tempor elit quis est deserunt cupidatat dolore. Mollit aliquip enim proident non commodo non sunt tempor nisi elit. Deserunt ex proident mollit esse. Ea Lorem sit ipsum occaecat ad ullamco.\r\n",
"registered": "2018-03-22T05:20:17 +07:00",
"latitude": 69.438455,
"longitude": 113.552142 };
var b = { "eyeColor": "bdrown",
"name": "Caitlind Beach",
"gender": "femalde",
"company": "MICRdOLUXE",
"email": "caitlindbeach@microluxe.com",
"phone": "+1 (954)d 462-3548", };
var c = Object.assign({}, a, b);
var a = { "_id": "624863161dde7aa8538857e6",
"index": 0,
"guid": "4be322b0-459c-41aa-bd09-f3227cd353df",
"isActive": false,
"balance": "$3,816.12",
"picture": "http://placehold.it/32x32",
"age": 26,
"eyeColor": "brown",
"name": "Caitlin Beach",
"gender": "female",
"company": "MICROLUXE",
"email": "caitlinbeach@microluxe.com",
"phone": "+1 (954) 462-3548",
"address": "854 Sapphire Street, Biddle, Colorado, 1002",
"about": "Adipisicing proident labore qui est minim tempor. Ipsum est cillum nisi exercitation laboris irure. Enim tempor elit quis est deserunt cupidatat dolore. Mollit aliquip enim proident non commodo non sunt tempor nisi elit. Deserunt ex proident mollit esse. Ea Lorem sit ipsum occaecat ad ullamco.\r\n",
"registered": "2018-03-22T05:20:17 +07:00",
"latitude": 69.438455,
"longitude": 113.552142 };
var b = { "eyeColor": "bdrown",
"name": "Caitlind Beach",
"gender": "femalde",
"company": "MICRdOLUXE",
"email": "caitlindbeach@microluxe.com",
"phone": "+1 (954)d 462-3548", };
var c = { a, b };
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash merge | |
object.assign | |
spread |
Test name | Executions per second |
---|---|
lodash merge | 1709183.1 Ops/sec |
object.assign | 1601761.8 Ops/sec |
spread | 8181083.0 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of three different methods to merge two objects in JavaScript:
_.merge
(Lodash library)Object.assign
Spread Operator
({...a, ...b}
)Options Compared
Each test case compares the execution time of a single run of each method.
Methods Breakdown
.merge
(Lodash): Uses the lodash.merge
function to merge two objects.Object.assign
: Uses the Object.assign()
method to create a new object and copy properties from the first object into it, then copies properties from the second object into the resulting object.Spread Operator
: Uses the spread operator ({...a, ...b}
) to merge two objects.Performance Results
The latest benchmark results show that:
spread
is the fastest method with an average execution time of 818,108,830 executions per second (Chrome 100)._.merge
is slower than spread
, but faster than Object.assign
. Its average execution time is 170,918,330 executions per second.Object.assign
is the slowest method, with an average execution time of 160,176,070 executions per second.Conclusion
The spread operator appears to be the most efficient way to merge two objects in JavaScript, followed closely by Lodash's .merge
function. The Object.assign
method is slower due to its overhead and the need to create a new object for each copy operation.