const elementAction = {
a: 1,
b: 2,
c: 3,
};
function func() {
switch (true) {
case Object.hasOwn(elementAction, 'foo'):
return false;
case Object.hasOwn(elementAction, 'bar'):
return false;
case Object.hasOwn(elementAction, 'baz'):
return false;
case Object.hasOwn(elementAction, 'bazz'):
return false;
case Object.hasOwn(elementAction, 'fooz'):
return false;
case Object.hasOwn(elementAction, 'foob'):
return false;
case Object.hasOwn(elementAction, 'doob'):
return false;
case Object.hasOwn(elementAction, 'bafz'):
return false;
case Object.hasOwn(elementAction, 'barz'):
return false;
case Object.hasOwn(elementAction, 'gas'):
return false;
case Object.hasOwn(elementAction, 'gaz'):
return false;
case Object.hasOwn(elementAction, 'daz'):
return false;
case Object.hasOwn(elementAction, 'dar'):
return false;
case Object.hasOwn(elementAction, 'har'):
return false;
case Object.hasOwn(elementAction, 'ooph'):
return false;
case Object.hasOwn(elementAction, 'phooph'):
return false;
default:
return true;
}
}
func();
const elementAction = {
a: 1,
b: 2,
c: 3,
};
const dictionary = [
{
key: 'foo',
value: () => false,
},
{
key: 'bar',
value: () => false,
},
{
key: 'baz',
value: () => false,
},
{
key: 'bazz',
value: false,
},
{
key: 'fooz',
value: false,
},
{
key: 'doob',
value: false,
},
{
key: 'bafz',
value: false,
},
{
key: 'barz',
value: false,
},
{
key: 'gas',
value: false,
},
{
key: 'gaz',
value: false,
},
{
key: 'daz',
value: false,
},
{
key: 'dar',
value: false,
},
{
key: 'har',
value: false,
},
{
key: 'ooph',
value: false,
},
{
key: 'phooph',
value: false,
},
{
key: 'foo',
value: false,
},
];
function func() {
const index = dictionary.indexOf(({ key }) => Object.hasOwn(elementAction, key));
return index > -1 ? dictionary[index].value : true;
}
func();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
switch/case | |
Dictionary |
Test name | Executions per second |
---|---|
switch/case | 725270.8 Ops/sec |
Dictionary | 11907991.0 Ops/sec |
Benchmark Overview
The benchmark compares the performance of two approaches: switch/case
and using an array (dictionary) with indexOf
.
Switch/Case Approach
In this approach, we use a switch
statement to check if a property exists in the elementAction
object. The case
statements have varying conditions, which are unlikely to be met, making it effectively equivalent to checking each property individually.
switch/case
. It can be more efficient if the number of properties in elementAction
is large.Dictionary Approach
In this approach, we use an array (dictionary
) with objects containing a key and a value function. We then use indexOf
to find the index of the first object in the dictionary where the key exists.
elementAction
. It can be more efficient for large numbers of properties since only the relevant objects need to be checked.Comparison and Considerations
When choosing between these approaches, consider the following:
elementAction
, the dictionary approach might be more efficient.elementAction
, the dictionary approach is easier to maintain.Other Alternatives
There are other approaches that can be used to check if a property exists in an object:
in
operator: This operator checks if a property exists directly in the object. It might offer better performance than exhaustive searches.hasOwnProperty
: While not as concise as using in
, this approach also checks for property existence.However, these alternatives may be less familiar or more complex to implement than the switch/case and dictionary approaches.