Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash omit + cloneDeep vs JS forEach
(version: 0)
Lodash omit + cloneDeep vs JS forEach
Comparing performance of:
Lodash omit + cloneDeep vs JS forEach
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Tests:
Lodash omit + cloneDeep
const a = {prop1: {a: 1}, prop2: {a: 2}}; const b = _.cloneDeep(_.omit(a, 'prop1')); b['prop2'] = null;
JS forEach
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash omit + cloneDeep
JS forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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`. ```javascript 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`. ```javascript 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** * **Lodash omit + cloneDeep** + Pros: - Concise and readable code - Leveraging Lodash's optimized `omit` function + Cons: - Additional dependency on Lodash library - May introduce overhead due to deep cloning * **JS forEach** + Pros: - No additional dependencies required - Uses built-in JavaScript features, potentially more efficient + Cons: - More verbose and less readable code - May not be as optimized as Lodash's implementation **Library: 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: 1. Using `Object.keys()` and manual iteration 2. Utilizing `reduce()` method for array-like objects 3. Leveraging `JSON.parse(JSON.stringify(obj))` for deep cloning However, 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.
Related benchmarks:
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign vs Clone
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign()
Object Clone Lodash vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?