<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
const a = {prop1: {a: 1}, prop2: {a: 2}};
const b = _.cloneDeep(_.omit(a, 'prop1'));
b['prop2'] = null;
const a = {prop1: {a: 1}, prop2: {a: 2}};
const b = {};
Object.keys(a).forEach(key => {
if(key !== 'prop1'){
b[key] = {a[key]};
}
});
b['prop2'].a = null;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash omit + cloneDeep | |
JS forEach |
Test name | Executions per second |
---|---|
Lodash omit + cloneDeep | 395995.2 Ops/sec |
JS forEach | 4639454.5 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is defined by two test cases: "Lodash omit + cloneDeep" and "JS forEach". The goal of this benchmark is to compare the performance of using Lodash's omit
function with cloneDeep
method against using a simple JavaScript forEach
loop to achieve similar results.
Test Case 1: Lodash omit + cloneDeep
This test case creates an object a
with two properties, each containing another object with a single property a
. The intention is to remove the prop1
property from object a
and then create a deep copy of the resulting object using cloneDeep
.
const a = {prop1: {a: 1}, prop2: {a: 2}};
const b = _.cloneDeep(_.omit(a, 'prop1'));
b['prop2'] = null;
Test Case 2: JS forEach
This test case achieves the same result as above but uses a forEach
loop to iterate over the object's properties and only keep the ones that are not equal to 'prop1'
. The resulting object is then assigned to variable b
.
const a = {prop1: {a: 1}, prop2: {a: 2}};
const b = {};
Object.keys(a).forEach(key => {
if (key !== 'prop1') {
tb[key] = {...a[key]};
}
});
b['prop2'].a = null;
Options Compared
The two test cases are compared in terms of performance. The benchmark measures the execution speed of each approach.
Pros and Cons
omit
functionLibrary: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string manipulation, and object transformation. In this benchmark, omit
is used to remove specified properties from an object, while cloneDeep
creates a deep copy of an object.
Special JS Feature/ Syntax: None
There are no special JavaScript features or syntax used in these test cases.
Other Alternatives
If you prefer not to use Lodash, alternative approaches could be:
Object.keys()
and manual iterationreduce()
method for array-like objectsJSON.parse(JSON.stringify(obj))
for deep cloningHowever, these alternatives might introduce additional complexity and potential performance overhead compared to the original implementation using Lodash's optimized functions.
In summary, the benchmark compares two approaches: using Lodash's omit
function with cloneDeep
method versus a simple JavaScript forEach
loop. The choice between these approaches depends on your specific use case, personal preference for code readability and complexity, and potential performance requirements.