<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
const a = { aa: 'oh', bb: 'my' };
const b = _.omit(a, 'aa');
let a = { aa: 'oh', bb: 'my' };
a = {a, aa: undefined};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash omit | |
spread omit |
Test name | Executions per second |
---|---|
lodash omit | 744297.2 Ops/sec |
spread omit | 12640192.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The benchmark measures the performance difference between two approaches for removing properties from an object in JavaScript:
_.omit
function from the Lodash library.{...obj, prop: undefined}
) to create a new object with the same properties as the original object, but with the specified property set to undefined
.Options compared
The two approaches are being compared for their performance differences.
Pros and cons of each approach:
{...obj, prop: undefined}
):_.omit
function for some users.Library: Lodash
Lodash is a popular JavaScript library that provides various utility functions for tasks like array manipulation, object processing, and more. The _omit
function is part of this library and returns a new object with the specified properties removed from the original object.
Special JS feature or syntax
This benchmark does not use any special JavaScript features or syntax beyond what's commonly used in modern JavaScript development (e.g., ES6+ syntax).
Other alternatives
If you want to remove properties from an object without using the Lodash library, you could also consider:
Object.keys()
and reduce()
methods to create a new object with only the desired keys.const obj = { aa: 'oh', bb: 'my' };
const newObj = Object.keys(obj).reduce((acc, key) => {
if (key !== 'aa') acc[key] = obj[key];
return acc;
}, {});
lodash
as an alternative to the spread operator approach.Keep in mind that these alternatives might have different performance characteristics compared to the Lodash library or the spread operator approach.