<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
var person = {name: 'Frederick', lastName: 'Corcino Alejo'};
_.assign({}, person, {age: 15});
Object.assign({}, person, {age: 15});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Assing | |
Native Assing |
Test name | Executions per second |
---|---|
Lodash Assing | 4155800.5 Ops/sec |
Native Assing | 11253544.0 Ops/sec |
Let's break down what's being tested in this JavaScript microbenchmark.
Benchmark Definition
The benchmark is comparing two approaches to perform an assignment operation on an object: Lodash.assign
and Object.assign
. The script preparation code creates a sample object person
with some properties, including a nested property lastName
.
Options Compared
Two options are being compared:
Pros and Cons of Each Approach
Object.assign
.In general, if you need to perform assignment operations on objects and don't care about additional features from Lodash, Object.assign
might be a better choice. However, if you anticipate working with complex data structures or want the convenience of handling nested objects, Lodash.assign
could be worth considering.
Library: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object creation, and string formatting. In this benchmark, Lodash.assign
is used to perform an assignment operation on the person
object.
Special JS Feature or Syntax: None Mentioned
There are no special JavaScript features or syntax being tested in this benchmark.
Other Alternatives
If you're looking for alternative ways to perform assignment operations on objects, you could consider:
Spread operator
({ ... }
) or Rest parameter
syntax (...object
) to create new objects.Object.keys()
and forEach()
.Keep in mind that these alternatives might have different performance characteristics or trade-offs, depending on the specific use case.