var testObject = {};
testObject.eligibilityGroupDates = [{
"id": "8c7969fd-56db-4ed8-9eec-5604c3dceef7",
"o2eStartDate": "scrub1",
"numOfDaysToCalO2EStartDate": null,
"numberOfDaysToEnroll": 99,
"eligibilityGroups": ["f32e6a9f-4a4b-4531-af8f-4e4ee0798e91"],
"links": []
}, {
"id": "7dc6052d-7ede-452a-8425-604c24c3d561",
"o2eStartDate": "scrub2",
"numOfDaysToCalO2EStartDate": 58,
"numberOfDaysToEnroll": 95,
"eligibilityGroups": ["f9987b9a-f45a-4131-8528-47242023483e"],
"links": []
}, {
"id": "af886791-fb79-4d28-921c-539cdaadae79",
"o2eStartDate": "scrub2",
"numOfDaysToCalO2EStartDate": null,
"numberOfDaysToEnroll": 30,
"eligibilityGroups": ["927a5c93-368c-4a82-a6d3-99e656624978"],
"links": []
}];
if (_.has(testObject, 'eligibilityGroupDates')) {
testObject.eligibilityGroupDates.forEach(entry => {
Object.keys(entry).forEach(key => {
switch (key) {
case 'eligibilityGroupDates':
_.set(testObject, 'displayEligibilityGroupDates', true);
break;
case 'o2eStartDate':
_.set(testObject, 'displayO2eStartDate', true);
break;
case 'numOfDaysToCalO2EStartDate':
_.set(testObject, 'displayNumOfDaysToCalO2EStartDate', true);
break;
case 'numberOfDaysToEnroll':
_.set(testObject, 'displayNumberOfDaysToEnroll', true);
break;
default:
break;
}
});
});
}
if (_.has(testObject, 'eligibilityGroupDates')) {
let value = JSON.stringify(testObject.eligibilityGroupDates);
if (value.indexOf('eligibilityGroups') > 0) {
_.set(testObject, 'eligibilityGroups', true);
}
if (value.indexOf('o2eStartDate') > 0) {
_.set(testObject, 'displayO2eStartDate', true);
}
if (value.indexOf('numOfDaysToCalO2EStartDate') > 0) {
_.set(testObject, 'displayNumOfDaysToCalO2EStartDate', true);
}
if (value.indexOf('numberOfDaysToEnroll') > 0) {
_.set(testObject, 'displayNumberOfDaysToEnroll', true);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
using loop | |
using JSON stringify |
Test name | Executions per second |
---|---|
using loop | 318245.2 Ops/sec |
using JSON stringify | 281773.3 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
The benchmark is designed to measure the performance of two different approaches for finding keys in an object: using a loop or by stringifying the object and searching for specific key values.
Options Compared
There are two main options being compared:
eligibilityGroupDates
array using a traditional loop, checking each property of each object in the array.eligibilityGroupDates
array and then searching for specific key values within that string.Pros and Cons
Library Used
The benchmark uses the lodash
library, specifically the _has
, _set
, and JSON.stringify
functions. Lodash is a popular utility library that provides a wide range of functions for working with arrays, objects, and strings in JavaScript.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark beyond the standard ECMAScript syntax. However, the use of _.has
, _set
, and JSON.stringify
from Lodash does require a basic understanding of these utility functions.
Other Alternatives
Some alternative approaches for finding keys in an object might include:
in
operator: Instead of using a loop or stringification, you could use the in
operator to check if a property exists within the object.In summary, this benchmark provides a simple and clear comparison between two approaches for finding keys in an object: using a loop versus using JSON stringify. By understanding the pros and cons of each approach, developers can make informed decisions about which method is best suited for their specific use case.