<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = _.assign(a, b);
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = Object.assign(a, b);
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = { a, b };
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash assign | |
object.assign | |
spread |
Test name | Executions per second |
---|---|
lodash assign | 4462793.0 Ops/sec |
object.assign | 6402160.5 Ops/sec |
spread | 18746944.0 Ops/sec |
Let's dive into the world of JavaScript benchmarking.
What is being tested?
The provided JSON represents a benchmark test that compares three different approaches to assigning properties from one object to another:
_assign
function from the Lodash library (a popular utility library for functional programming in JavaScript).Object.assign
method, which is a part of the ECMAScript 2015 (ES6) standard.{ ...object1, ...object2 }
) to create a new object with properties from both objects.What options are compared?
The three test cases compare different ways to assign properties from one object (b
) to another (a
):
a
and b
are initialized as objects: var a = { a: 'oh', b: 'my' };\n var b = { c: 'goddess' };
.b
to a
, using:_assign
function (var c = _.assign(a, b);
)Object.assign
method (var c = Object.assign(a, b);
)var c = { ...a, ...b };
)Pros and Cons of each approach:
Object.assign
for large datasets.Other considerations:
{ ...a, ...b }
) is a newer feature in JavaScript (introduced in ES6) and may not work correctly in older browsers or environments._assign
function provides more advanced features (like merging objects with arrays), which might be useful depending on your use case.What library is used?
The lodash
library is a popular utility library for functional programming in JavaScript. It provides various functions, including the _assign
method, to make working with data structures easier and more efficient.
That's it!