Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find Keys by loop or JSON stringify
(version: 0)
Comparing performance of:
using loop vs using JSON stringify
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
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": [] }];
Tests:
using loop
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; } }); }); }
using JSON stringify
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); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
using loop
using JSON stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
using loop
1738436.6 Ops/sec
using JSON stringify
1048874.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Using a loop**: This approach involves iterating over the `eligibilityGroupDates` array using a traditional loop, checking each property of each object in the array. 2. **Using JSON stringify**: This approach involves stringifying the entire `eligibilityGroupDates` array and then searching for specific key values within that string. **Pros and Cons** 1. **Using a loop**: * Pros: More straightforward and easier to understand, as it directly accesses each property of each object in the array. * Cons: May be slower due to the overhead of iterating over the array and checking each property individually. 2. **Using JSON stringify**: * Pros: Can potentially be faster if the stringification process is more efficient than iterating over the array, especially for large datasets. * Cons: Requires additional processing time to convert the object into a string, which may add overhead. **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: 1. **Using the `in` operator**: Instead of using a loop or stringification, you could use the `in` operator to check if a property exists within the object. 2. **Using a regular expression**: If you need to search for multiple properties across the entire object, you might consider using a regular expression to match against the desired keys. 3. **Using a more specialized data structure**: Depending on the specific requirements of your application, you might be able to use a more specialized data structure, such as an indexed array or a trie, to improve performance. 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.
Related benchmarks:
find in object array vs find in string array vs includes string array
Lodash sortBy vs orderBy two items
Array.find vs Array.some array of objects
json parse and search regex1
Comments
Confirm delete:
Do you really want to delete benchmark?