<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js'></script>
var allHeaders = {
'Accept-Encoding': 'gzip,deflate',
'Accept-Language': 'en-US',
'Content-Type': 'application/json',
'User-Agent': `(compatible; Mozilla/5.0; MSIE 9.0; Trident/5.0;})`,
'Proxy-Connection': 'Keep-Alive',
Connection: 'Keep-Alive',
Accept: 'application/hal+json, application/json',
};
function omit(obj, keys) {
var result = {};
for (let i in obj) {
if (keys.indexOf(i) >= 0) continue;
result[i] = obj[i];
}
return result;
}
_.omit(allHeaders, ['Accept-Encoding', 'User-Agent', 'Connection']);
omit(allHeaders, ['Accept-Encoding', 'User-Agent', 'Connection']);
_.omit(allHeaders, ['Accept-Encoding', 'Connection']);
omit(allHeaders, ['Accept-Encoding', 'Connection']);
_.omit(allHeaders, ['Connection']);
omit(allHeaders, ['Connection']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash omit for 3 keys | |
native for in loop for 3 keys | |
lodash omit for 2 keys | |
native for in loop for 2 keys | |
lodash omit for 1 keys | |
native for in loop for 1 keys |
Test name | Executions per second |
---|---|
lodash omit for 3 keys | 582483.4 Ops/sec |
native for in loop for 3 keys | 3575706.0 Ops/sec |
lodash omit for 2 keys | 519432.3 Ops/sec |
native for in loop for 2 keys | 3290181.5 Ops/sec |
lodash omit for 1 keys | 522757.9 Ops/sec |
native for in loop for 1 keys | 2997678.2 Ops/sec |
Measuring the performance of JavaScript functions is crucial in ensuring optimal code execution.
Benchmark Overview
The provided benchmark compares three approaches to remove specific keys from an object:
omit
functionfor-in-loop
approach...
) with Object.fromEntries
Each test case uses the same input data, but varies the number of keys to be omitted.
Lodash omit
function
The Lodash omit
function is a utility function that removes specified properties from an object. It takes two arguments: the object to modify (obj
) and an array of property names to exclude (keys
). The function returns a new object with only the included properties.
Pros:
Cons:
Native for-in-loop
approach
This approach uses a traditional for-in-loop
to iterate over the object's properties and only includes those that are not present in the keys
array. This method is more explicit and easier to understand.
Pros:
Cons:
ES6 rest spread operator (...
) with Object.fromEntries
This approach uses the rest spread operator (...
) to create a new array of key-value pairs, excluding those that are present in the keys
array. The resulting array is then passed to Object.fromEntries
, which creates a new object from it.
Pros:
Cons:
Comparison of Approaches
Based on the benchmark results, the ES6 rest spread operator (...
) with Object.fromEntries
appears to be the fastest approach, followed closely by the native for-in-loop
approach. The Lodash omit
function is slower due to the overhead of creating a new object.
Other Considerations
for-in-loop
approach may be a better choice.Alternatives
For similar benchmarking scenarios:
Object.fromEntries
alone without the rest spread operator for a more explicit approachomit
using a native loop instead of LodashKeep in mind that these alternatives may not offer significant performance improvements, but they can provide alternative perspectives on the problem.