Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Omit lodash vs plain function
(version: 0)
Comparing performance of:
lodash omit vs plain omit (structuredClone) vs plain omit (JSON.stringify)
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function flattenKeys(s) { return s.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '').split('.') } var testA = { a: { b: { c: { d: [{ sample1: 'sample1', sample2: { sample3: { sample4: 'sample4' } } }, { otherSample1: 'otherSample1', otherSample2: { otherSample3: { otherSample4: 'otherSample4' } } }] } } } }; function omit3(obj, props) { if (typeof props == "string") { props = [props]; } if (obj != null && obj != undefined && typeof obj == 'object') { const newObj = JSON.parse(JSON.stringify(obj)); for (const i in props) { const path = flattenKeys(props[i]); if (path.length == 1) { if (typeof newObj == 'object') { if (Array.isArray(newObj) && newObj?. [props[i]] != null) { newObj?.splice(props[i], 1); } else { delete newObj?. [props[i]]; } } } else if (path.length > 1) { if (newObj[path[0]] != null) { newObj[path[0]] = omit3(newObj?. [path[0]], [path.slice(1).join('.')]); } } } return newObj; } return obj; } function omit2(obj, props) { if (typeof props == "string") { props = [props]; } if (obj != null && obj != undefined && typeof obj == 'object') { const newObj = structuredClone(obj); for (const i in props) { const path = flattenKeys(props[i]); if (path.length == 1) { if (typeof newObj == 'object') { if (Array.isArray(newObj) && newObj?. [props[i]] != null) { newObj?.splice(props[i], 1); } else { delete newObj?. [props[i]]; } } } else if (path.length > 1) { if (newObj[path[0]] != null) { newObj[path[0]] = omit2(newObj?. [path[0]], [path.slice(1).join('.')]); } } } return newObj; } return obj; }
Tests:
lodash omit
const resultA = _.omit(testA, ['a.b.c.d']); const resultB = _.omit(testA, ['a.b.c.d[1].otherSample2.otherSample3']);
plain omit (structuredClone)
const resultA = omit2(testA, ['a.b.c.d']); const resultB = omit2(testA, ['a.b.c.d[1].otherSample2.otherSample3']);
plain omit (JSON.stringify)
const resultA = omit3(testA, ['a.b.c.d']); const resultB = omit3(testA, ['a.b.c.d[1].otherSample2.otherSample3']);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash omit
plain omit (structuredClone)
plain omit (JSON.stringify)
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):
The provided benchmark measures the performance of two approaches to remove specific properties from an object: using Lodash's `omit` function and two alternative plain JavaScript implementations, `omit3` and `omit2`. **Lodash `omit`** * The Lodash `omit` function is used in the first test case. * It takes two arguments: the object to modify (`testA`) and an array of property paths to remove. **Plain JavaScript implementations (omit3 and omit2)** * `omit3`: * Uses a recursive approach, where it checks if the current path is an array or an object. If it's an array, it removes the specified element; otherwise, it recursively calls itself with the nested object. * `omit2`: * Also uses a recursive approach but employs `structuredClone()` to create a deep copy of the original object before modifying it. **Comparison and pros/cons** The three approaches are compared in terms of execution speed. The results indicate that: * Lodash's `omit` function is significantly faster than both plain JavaScript implementations. * `omit3` and `omit2` have similar performance characteristics, with `omit3` being slightly faster. **Pros and cons of each approach** * **Lodash `omit`** * Pros: + Highly optimized for performance + Easy to use and understand * Cons: + Additional dependency on Lodash library + May not be suitable for situations where a lightweight solution is required * **`omit3`** * Pros: + Lightweight solution with no additional dependencies + Simple and easy to understand implementation * Cons: + Performance may not be as good as Lodash's `omit` + Recursive approach can lead to stack overflow for very deep objects * **`omit2`** + Pros: + Similar performance characteristics to `omit3` + Uses a lightweight and optimized implementation (`structuredClone`) * Cons: + Additional dependency on the `structuredClone()` function **Other considerations** * The benchmark assumes that the input object is structured in a nested manner, which can impact the performance of the removal process. * Other factors like object size, complexity, and data type may also influence the performance of each approach. In conclusion, while Lodash's `omit` function offers excellent performance, plain JavaScript implementations (`omit3` and `omit2`) can provide a lightweight alternative. However, it is essential to consider the specific requirements and constraints of your project before choosing an implementation.
Related benchmarks:
lodash omit vs. spread omit
lodash omit vs spread omit vs delete omit
Lodash omit vs Native object destruction
lodash omit vs spread omit modified
lodas pick vs plain function
Comments
Confirm delete:
Do you really want to delete benchmark?