<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
data = {
abf: 'asd',
message: 'koko',
_id: '{{objectId()}}',
index: '{{index()}}',
guid: '{{guid()}}',
isActive: '{{bool()}}',
balance: '{{floating(1000, 4000, 2, "$0,0.00")}}',
picture: 'http://placehold.it/32x32',
age: '{{integer(20, 40)}}',
eyeColor: '{{random("blue", "brown", "green")}}',
name: '{{firstName()}} {{surname()}}',
gender: '{{gender()}}',
company: '{{company().toUpperCase()}}',
email: '{{email()}}',
phone: '+1 {{phone()}}',
address: '{{integer(100, 999)}} {{street()}}, {{city()}}, {{state()}}, {{integer(100, 10000)}}',
about: '{{lorem(1, "paragraphs")}}',
registered: '{{date(new Date(2014, 0, 1), new Date(), "YYYY-MM-ddThh:mm:ss Z")}}',
latitude: '{{floating(-90.000001, 90)}}',
longitude: '{{floating(-180.000001, 180)}}'
}
keysToOmit = ['longitude', 'gender', 'guid', 'abf']
Object.keys(data).forEach((key) => {
keysToOmit.includes(key) && delete data[key];
});
return {
data
}
return {
data: _.omit(data, keysToOmit)
}
var {longitude, gender, guid, abf, rest} = data
return {
data: rest
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native delete | |
Lodash omit | |
ES6 Destructure |
Test name | Executions per second |
---|---|
Native delete | 341682.9 Ops/sec |
Lodash omit | 276979.1 Ops/sec |
ES6 Destructure | 338724.3 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is comparing three approaches to remove specific keys from an object:
delete
operator to remove keys from the object.omit
function from the Lodash library to remove keys from the object.Options being compared
delete
operator to remove keys from the object. It's a simple and straightforward way to achieve this, but it may not be as efficient or safe as other methods.omit
function, which is designed to be fast and efficient. This approach leverages the library's expertise in handling complex data manipulation tasks.Pros and Cons of each approach
Library: Lodash
Lodash is a popular JavaScript library that provides a comprehensive set of utilities for data manipulation, functional programming, and more. The omit
function is one of its most useful tools for removing specific keys from objects. In this benchmark, Lodash is used as a third-party library to provide an optimized implementation of the omit
function.
Special JS feature: Destructuring assignment
Destructuring assignment is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). It allows developers to extract values from objects and arrays using a concise syntax. In this benchmark, destructuring assignment is used as an alternative approach to removing unwanted keys from the object.
Other alternatives
For those who may not be familiar with Lodash or destructuring assignment, here are some additional approaches that could be considered:
for...in
loop: An older approach that iterates over the object's properties using a for...in
loop and removes unwanted keys manually.Object.keys()
and forEach()
: Another older approach that uses Object.keys()
to get an array of property names, then iterates over the array using forEach()
and removes unwanted keys.These alternatives may not be as efficient or concise as the approaches used in this benchmark but can still be viable options depending on the specific use case.