<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js</script>
var obj = {
a:{
b:{
c:{
d:{
e: 20
}
}
}
}
};
_.has(obj, 'a.b.c.d.e');
obj.a.b.c.d.hasOwnProperty('e')
!!((((obj.a || ob).b || ob).c || ob).d).e;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Javascript Object | |
Plain Javascript |
Test name | Executions per second |
---|---|
Lodash | 2154678.8 Ops/sec |
Javascript Object | 13637568.0 Ops/sec |
Plain Javascript | 15890761.0 Ops/sec |
I'd be happy to help you understand the benchmark being tested on MeasureThat.net.
What is being tested?
The benchmark tests three different approaches for checking if an object has a specific property with a value:
_.has()
function from the Lodash library, which checks if an object has a specific property.in
operator to check if an object has a specific property, and then using the .hasOwnProperty()
method to verify that the property is not inherited from a prototype.obj.a.b.c.d.e
) to access the nested properties of the object.Options compared
The three approaches are compared in terms of their performance, measured by the number of executions per second on a specific device (Chrome 85 on a Mac OS X 10.15.5 desktop).
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
in
operator and hasOwnProperty()
method.Library usage
The benchmark uses the Lodash library, which is a popular utility library for functional programming in JavaScript. The _.has()
function is used to check if an object has a specific property.
Special JS feature or syntax
None of the approaches use any special JavaScript features or syntax that would be unfamiliar to most software engineers. However, it's worth noting that the in
operator and hasOwnProperty()
method are part of the ECMAScript standard, so they may not require additional dependencies or setup.
Other alternatives
If you're interested in exploring alternative approaches, here are a few options:
Keep in mind that each approach has its trade-offs, and the choice ultimately depends on the specific requirements of your use case.