var jsonEntity = {"ID":1,"MAINSOURCESYSTEM":null,"EXTERNALCODE":"VASCO1","NAME":"","FIRSTNAME":"Vasco","LASTNAME":"Mendes","SEARCHNAME1":"","SEARCHNAME2":"","CONTACTFORM":"Sr.","NIF":"211111111","COUNTRY":"Portugal","D":"","addresses":[{"ID":2,"BPID":1,"ADDRESS1":"Rua Morada 1 do Vasco","ADDRESS2":"Rua Morada 2 do Vasco","NUMBER":"nº 341","LOCALE":"Vale do Porto","POSTCODE":"2496-660","CITY":"Fátima","STATE":"Ourém-Santarém","COUNTRY":"Portugal","ISMAINADDRESS":true,"ISACTIVE":true,"VALIDFROM":"1900-01-01T00:00:00","VALIDTO":"1900-01-01T00:00:00","DESCRIPTION":"Morada principal"},{"ID":13,"BPID":1,"ADDRESS1":"Algarve em grande casas","ADDRESS2":"Junto ao mar","NUMBER":"No areal","LOCALE":"Algarve","POSTCODE":"2222-222","CITY":"Algarve","STATE":"","COUNTRY":"Portugal","ISMAINADDRESS":false,"ISACTIVE":true,"VALIDFROM":"1900-01-01T00:00:00","VALIDTO":"1900-01-01T00:00:00","DESCRIPTION":"Morada de Férias"}],"contacts":[{"ID":10,"BPID":1,"TYPE":1,"CONTACTNAME":"Vasco Mendes","CONTACTTITLE":"Sr,","CONTACT":"919999999","LANG":"PT","ISMAINCONTACTFORM":true,"ISMAINCONTACT":true,"ISACTIVE":true,"VALIDFROM":"1900-01-01T00:00:00","VALIDTO":"1900-01-01T00:00:00"},{"ID":11,"BPID":1,"TYPE":3,"CONTACTNAME":"Vasco","CONTACTTITLE":"Sr","CONTACT":"vasco.mendes@rupolusiaves.pt","LANG":"PT","ISMAINCONTACTFORM":false,"ISMAINCONTACT":false,"ISACTIVE":true,"VALIDFROM":"1900-01-01T00:00:00","VALIDTO":"1900-01-01T00:00:00"},{"ID":12,"BPID":1,"TYPE":2,"CONTACTNAME":"Responsável de departamento de compras","CONTACTTITLE":"","CONTACT":"+3519919191919","LANG":"PT","ISMAINCONTACTFORM":false,"ISMAINCONTACT":false,"ISACTIVE":true,"VALIDFROM":"1900-01-01T00:00:00","VALIDTO":"1900-01-01T00:00:00"}]};
_.get(jsonEntity, 'addresses[1].ADDRESS1');
jsonEntity.addresses[1].ADDRESS1
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Address Lodash | |
Native |
Test name | Executions per second |
---|---|
Address Lodash | 1908128.9 Ops/sec |
Native | 10201449.0 Ops/sec |
Let's dive into the world of JavaScript benchmarks!
Benchmark Definition JSON
The provided JSON represents a benchmark definition for a test case named LodashBigStructure(VM1)
. The main object contains information about the benchmark, such as its name and description.
There are two child objects: Script Preparation Code
and Html Preparation Code
. However, in this case, both fields are empty (null
).
The key part of the benchmark is the array of individual test cases:
[
{
"Benchmark Definition": "_.get(jsonEntity, 'addresses[1].ADDRESS1');\t",
"Test Name": "Address Lodash"
},
{
"Benchmark Definition": "jsonEntity.addresses[1].ADDRESS1",
"Test Name": "Native"
}
]
These test cases are comparing the execution time of two different ways to access a property in an object:
_.get()
function, which is a utility function for safely navigating and retrieving values from objects.jsonEntity.addresses[1].ADDRESS1
).Options Comparison
Let's analyze the pros and cons of each approach:
Pros:
_get()
is designed to prevent null pointer exceptions by checking if the object and path exist._.get(obj, 'a.b[c]')
).Cons:
Pros:
Cons:
Library: Lodash
Lodash is a popular JavaScript library that provides a wide range of utility functions for tasks like array manipulation, object traversal, and more. The _get()
function is one of its most useful utilities, allowing you to safely navigate objects and retrieve values.
Special JS Feature/Syntax: None
There are no special JavaScript features or syntax used in these test cases.
Other Alternatives
If you need to traverse nested objects, other alternatives might include:
json-path
to specify paths for object traversal.Object.prototype.hasOwnProperty.call()
for safe property access.Keep in mind that the choice of approach depends on the specific requirements of your project, such as performance, safety, and code readability.