<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
var object = { 'a': 1, 'b': '2', 'c': 3 };
var { a, c, result2 } = object;
return result2
return _.omit(object, ['a', 'c']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native object desctruction | |
Lodash omit |
Test name | Executions per second |
---|---|
Native object desctruction | 13299454.0 Ops/sec |
Lodash omit | 1099140.1 Ops/sec |
Benchmark Explanation
The provided benchmark measures the performance difference between two approaches to achieve object destruction in JavaScript: native object destruction and using the Lodash library's omit
function.
Native Object Destruction
In this approach, an object is created with three properties (a
, b
, and c
) and then destructured using a syntax similar to ES6 destructuring. The resulting object without the unwanted properties (result2
) is returned from the function. This approach relies on JavaScript's native syntax for object destruction.
Pros of Native Object Destruction
Cons of Native Object Destruction
Lodash Omit
In contrast, the Lodash library's omit
function is used to achieve object destruction. This approach involves calling a separate library function and passing an array of keys to exclude from the original object.
Pros of Lodash Omit
omit
can be straightforward and efficient.Cons of Lodash Omit
Library Description - Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object modification, and more. The omit
function specifically returns a new object with the specified keys removed from the original object.
Other Considerations
Alternatives to Native Object Destruction
If native object destruction is not an option, other approaches could include:
Object.assign()
with a new object that excludes unwanted properties.delete
function for objects.Keep in mind that these alternatives may have their own trade-offs in terms of efficiency, browser support, and overall performance.