<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js'></script>
var a = {
"development": {
"appName": "abcd",
"storeListEndpoint": "/data/stores.json",
"filters": {
"filtersIcon": "https://icon.org/filters.svg",
"closeIcon": "https://icon.org/close.svg"
},
"maps": {
"googleMaps": "https://maps.googleapis.com/maps/api/js",
"mapsIcon": "https://icon.org/store-map-marker.png",
"mapsIcon": "https://icon.org/store-map-marker.png",
"defaultZoom": 6,
"zoomControl": true
},
"routing": {
"basepath": false,
"stores": "stores",
"store": "store"
},
"features": {
"searchLocation": false,
"geoLocation": false,
"showLandingPage": false,
"displayInfoWindow": false
}
},
"test": {
"appName": "tyuiooo"
},
"production": {
"appName": "bvbvbvvb",
"storeListEndpoint": "https://www.api.com/?stores_action_get_stores",
"routing": {
"basepath": "/poipoi"
}
}
};
var b = {
production: {
appName: "works ok"
}
};
var c = _.assign(a, b);
var a = {
"development": {
"appName": "abcd",
"storeListEndpoint": "/data/stores.json",
"filters": {
"filtersIcon": "https://icon.org/filters.svg",
"closeIcon": "https://icon.org/close.svg"
},
"maps": {
"googleMaps": "https://maps.googleapis.com/maps/api/js",
"mapsIcon": "https://icon.org/store-map-marker.png",
"mapsIcon": "https://icon.org/store-map-marker.png",
"defaultZoom": 6,
"zoomControl": true
},
"routing": {
"basepath": false,
"stores": "stores",
"store": "store"
},
"features": {
"searchLocation": false,
"geoLocation": false,
"showLandingPage": false,
"displayInfoWindow": false
}
},
"test": {
"appName": "tyuiooo"
},
"production": {
"appName": "bvbvbvvb",
"storeListEndpoint": "https://www.api.com/?stores_action_get_stores",
"routing": {
"basepath": "/poipoi"
}
}
};
var b = {
production: {
appName: "works ok"
}
};
var c = Object.assign(a, b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash merge | |
object.assign |
Test name | Executions per second |
---|---|
lodash merge | 2503016.0 Ops/sec |
object.assign | 4192525.2 Ops/sec |
Let's dive into the benchmark.
Benchmark Definition
The benchmark is comparing two ways to assign or merge objects: _.assign
from Lodash and Object.assign
.
Options compared
_.assign
: A function from the Lodash library that merges multiple objects into one. It takes an array of source objects as its argument.Object.assign
: A built-in JavaScript method that copies properties from one or more source objects to a target object.Pros and Cons of each approach
_.assign
:Object.assign
:_.assign
.Lodash library
The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks like string manipulation, array operations, and object manipulation. In this case, it's used for the _.assign
function, which allows for more flexible and powerful merging.
Special JS feature or syntax
There are no special JS features or syntax mentioned in the benchmark definition. However, it's worth noting that Lodash uses a functional programming style, where functions like _.assign
return new values rather than modifying existing ones. This can be beneficial for predictable behavior and ease of testing.
Other alternatives
If you don't want to use Lodash, you could consider using other libraries or built-in methods for merging objects. Some examples include:
merge
from the lodash-es
library (a more modern version of Lodash with ES6 compatibility)Object.assign()
with additional arguments (e.g., { ...source, ...target }
)Keep in mind that each alternative has its pros and cons, and some might be more suitable for specific use cases than others.
Test case explanation
The test case is creating two objects, a
and b
, with nested structures. It then uses _.
assignto merge these objects into a new object,
c. The resulting object should have properties from both
aand
b`.
For the object.assign
version of the test case, the same approach is used, but without the Lodash library.
Benchmark results
The benchmark results show the raw UA string (browser type and version), device platform, operating system, executions per second, and the test name. The numbers indicate that Object.assign
performed slightly better in terms of execution speed compared to _.
assign`.