<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script src=''>
</script>
var omit1 = (originalObj = {}, keysToOmit = []) =>
Object.fromEntries(
Object.entries(originalObj)
.filter(([key]) => !keysToOmit.includes(key))
)
var omit2 = new Function('obj', 'if (!obj) return {}; const { a, d, i, ...res } = obj; return res;');
var omit3 = (originalObject = {}, keysToOmit = []) => {
const clonedObject = { originalObject };
for (const path of keysToOmit) {
delete clonedObject[path]
}
return clonedObject;
}
function omit4(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
}
const obj = {
a:1,
b:1,
c:1,
d:1,
e:1,
f:1,
g:1,
h:1,
i:1,
}
const n = _.omit(obj, ['a','d','i']);
const obj = {
a:1,
b:1,
c:1,
d:1,
e:1,
f:1,
g:1,
h:1,
i:1,
}
const n = omit1(obj, ['a','d','i']);
const obj = {
a:1,
b:1,
c:1,
d:1,
e:1,
f:1,
g:1,
h:1,
i:1,
}
const n = omit2(obj, ['a','d','i']);
const obj = {
a:1,
b:1,
c:1,
d:1,
e:1,
f:1,
g:1,
h:1,
i:1,
}
const n = omit3(obj, ['a','d','i']);
const obj = {
a:1,
b:1,
c:1,
d:1,
e:1,
f:1,
g:1,
h:1,
i:1,
}
const n = omit4(obj, ['a','d','i']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
es omit | |
compiled omit | |
rest and delete omit | |
babel |
Test name | Executions per second |
---|---|
Lodash | 486282.3 Ops/sec |
es omit | 1320568.9 Ops/sec |
compiled omit | 974613.6 Ops/sec |
rest and delete omit | 2121455.5 Ops/sec |
babel | 787476.4 Ops/sec |
Benchmark Overview
The provided benchmark, hosted on MeasureThat.net, compares the performance of five different JavaScript functions for removing properties from an object: lodash.omit
, es omit
, compiled omit
, rest and delete omit
, and babel
.
Tested Options
Here are the options being compared:
omit
: A widely-used JavaScript library that provides a simple way to remove properties from an object.omit
): A built-in JavaScript feature introduced in ECMAScript 2020, which allows you to remove properties from an object using the omit
function.omit
function, likely generated by a tool like Babel, which transpiles modern JavaScript code to older versions that are supported by more browsers....
) to extract properties from an object and then deletes them using the delete
operator.omit
function.Pros and Cons
Here's a brief summary of the pros and cons of each option:
omit
:omit
):omit
, as it's optimized for older browsers.delete
operator.Latest Benchmark Results
The latest benchmark results show that:
omit
) comes in second with 1320568.875 executions per second.omit
performs the worst with 2121455.5 executions per second.Please note that these results may vary depending on the specific use case, browser, and environment.