<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');
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var a = {
aa: 'oh',
bb: 'my'
};
var aa = a.aa,
b = _objectWithoutProperties(a, ["aa"]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash omit | |
spread as compiled by babel |
Test name | Executions per second |
---|---|
lodash omit | 563338.6 Ops/sec |
spread as compiled by babel | 1328902.6 Ops/sec |
Let's break down the provided JSON data to understand what's being tested.
Benchmark Definition
The benchmark measures the performance of two approaches:
omit
: This test case uses Lodash, a popular JavaScript utility library, to omit specific keys from an object. The test creates an object a
with two properties: aa
and bb
. It then calls _omit
function on this object, passing 'aa'
as the excluded key.{...}
) to create a new object that excludes specific keys from the original object a
.Options Compared
The two options being compared are:
omit
: A library-based approach that uses Lodash's implementation of the omit
function.Pros and Cons
Lodash omit
Pros:
omit
functionCons:
Spread operator (compiled by Babel)
Pros:
Cons:
Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions, including omit
, map
, filter
, and many others. Its implementation is highly optimized and tested, making it a reliable choice for many use cases.
Special JS Feature/Syntax: None mentioned
There are no special JavaScript features or syntax being used in these test cases.
Other Alternatives
For the omit
function, other alternatives include:
For the spread operator, other alternatives include:
Object.assign()
or Array.prototype.slice()
)omit
function)In general, the choice of approach depends on the specific use case, performance requirements, and personal preference.