<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
data = { a:'koko',b:'koko',c:'koko',d:'koko',e:'koko',f:'koko',g:'koko',h:'koko',i:'koko',x:'asd' }
['a','x'].map(key => delete data.key)
return data
return _.omit(data, ['a','x'])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Delete of map | |
Omit |
Test name | Executions per second |
---|---|
Delete of map | 18609340.0 Ops/sec |
Omit | 1182343.6 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Overview
The benchmark measures the performance difference between using delete
to remove properties from an object (_.omit
) and using Object.keys().filter()
(along with Lodash's omit
function) in JavaScript.
Options being compared
Two approaches are being compared:
delete data.key
to remove properties from the data
object._omit
function to filter out specific keys from the data
object.Pros and Cons of each approach:
Direct Deletion (Delete)
Pros:
Cons:
data.a
would delete a
from both the top-level and any nested objects).Lodash's Omit
Pros:
Cons:
Library usage
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like string manipulation, array processing, and more. In this benchmark, Lodash's _omit
function is used to filter out specific keys from the data
object.
Special JS feature/syntax
None mentioned in this explanation. However, it's worth noting that some JavaScript features or syntax might affect the performance of these approaches (e.g., using let
or const
instead of var
for variable declarations). But in this case, no specific features are being leveraged.
Other alternatives
For those looking to optimize object removal in JavaScript, consider:
omit
function, as it provides a more predictable and robust way to filter objects.Object.keys()
or for...in
.Map
or Set
to represent the object's properties (if available in older browsers).Keep in mind that performance optimizations should always be evaluated on a per-project basis, considering factors like data size, complexity, and specific use cases.