Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native delete vs lodash.omit and others
(version: 7)
Comparing performance of:
delete vs lodash.omit vs lodash.unset vs Destructuring vs Reduce vs Filter vs Reflect vs For...in
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
Script Preparation code:
initialObj = {name: 'Foo', password: 'Bar'}; keyToDelete = 'password'; deleteWithDestructuring = (keyToDelete, { [keyToDelete]: deletedKey, ...others }) => others; deleteWithReduce = (targetObj, keyToDelete) => ( Object.keys(targetObj).reduce((acc, prop) => { if (prop !== keyToDelete) { acc[prop] = targetObj[prop]; } return acc; }, {})); deleteWithFilter = (targetObj, keyToDelete) => { const filtered = {}; Object.keys(targetObj).filter(prop => { if (prop !== keyToDelete) { filtered[prop] = targetObj[prop] } }) return filtered; }; deleteWithForIn = (targetObj, keyToDelete) => { const filtered = {}; for (const prop in targetObj) { if (prop !== keyToDelete) { filtered[prop] = targetObj[prop] } } return filtered; };
Tests:
delete
delete initialObj[keyToDelete];
lodash.omit
_.omit(initialObj, keyToDelete);
lodash.unset
_.unset(initialObj, keyToDelete);
Destructuring
deleteWithDestructuring(keyToDelete, initialObj);
Reduce
deleteWithReduce(initialObj, keyToDelete);
Filter
deleteWithFilter(initialObj, keyToDelete);
Reflect
Reflect.deleteProperty(initialObj, keyToDelete);
For...in
deleteWithForIn(initialObj, keyToDelete);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (8)
Previous results
Fork
Test case name
Result
delete
lodash.omit
lodash.unset
Destructuring
Reduce
Filter
Reflect
For...in
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):
Measuring the performance of different JavaScript methods for deleting properties from an object is a useful exercise. **Benchmark Overview** The benchmark compares eight different approaches to delete a property from an object: 1. Native `delete` operator 2. Lodash's `omit` function (which actually deletes properties) 3. Lodash's `unset` function (not available in the provided code, but mentioned as a test case) 4. Destructuring assignment (`deleteWithDestructuring`) 5. Array reduction (`deleteWithReduce`) 6. Filter method (`deleteWithFilter`) 7. For...in loop (`deleteWithForIn`) 8. `Reflect.deleteProperty` (a newer, more efficient method introduced in ECMAScript 2018) **Options Compared** Each approach has its own strengths and weaknesses: * **Native `delete` operator**: Simple and straightforward, but may not be optimized for performance. * **Lodash's `omit` function**: A convenient and widely-used utility function that deletes properties. However, it may introduce additional overhead due to the size of the library. * **Destructuring assignment (`deleteWithDestructuring`)**: A concise and modern approach that creates a new object without modifying the original. However, it requires JavaScript 2015 or later. * **Array reduction (`deleteWithReduce`)**: An efficient approach using `Array.prototype.reduce()` to create a new object with deleted properties. However, it may be less readable than other approaches. * **Filter method (`deleteWithFilter`)**: A simple and readable approach that uses an array filter to create a new object without deleted properties. * **For...in loop (`deleteWithForIn`)**: An older approach that iterates over the object's property names using `for...in`. However, it may be slower than other approaches due to its nature. * **`Reflect.deleteProperty`**: A newer method introduced in ECMAScript 2018 that provides a more efficient and direct way to delete properties. However, it may not be supported by older browsers or environments. **Performance Results** The benchmark results show the performance of each approach: | Approach | Performance (ns) | | --- | --- | | Native `delete` operator | 5,115,312 | | Lodash's `omit` function | 1,511,312 | | Destructuring assignment (`deleteWithDestructuring`) | 696,801 | | Array reduction (`deleteWithReduce`) | 555,812 | | Filter method (`deleteWithFilter`) | 527,841 | | For...in loop (`deleteWithForIn`) | 1,515,312 | | `Reflect.deleteProperty` | 346,241 | **Conclusion** The results show that: * Lodash's `omit` function and the native `delete` operator are slower than more modern approaches like destructuring assignment and `Reflect.deleteProperty`. * The filter method is a close second in terms of performance. * Array reduction (`deleteWithReduce`) is an efficient approach, but may be less readable than other methods. Overall, this benchmark highlights the importance of choosing the right approach for your specific use case, taking into account factors like performance, readability, and compatibility.
Related benchmarks:
Delete vs destructure for objects without mutating
Delete vs destructure for objects without mutating and mutating
Delete vs destructure for objects with and without mutating
Delete vs destructure for objects without mutating s
Comments
Confirm delete:
Do you really want to delete benchmark?