var ids = [''];
var entities = { '': { id: '', number: 10000 } };
var id = '';
for (let i = 0; i < 10000; i++) {
id = String(i);
ids.push(id);
entities[id] = { id, number: i }
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
var idsFind = [''];
for (let i = 0; i < 100; i++) {
idsFind.push(String(getRandomInt(0, 20000)));
}
var result = [];
for (const id of idsFind) {
if (ids.includes(id)) {
result.push(entities[id])
}
}
return result
var result = [];
for (const id of idsFind) {
if (Object.prototype.hasOwnProperty.call(entities, id)) {
result.push(entities[id])
}
}
return result
var result = [];
for (const id of idsFind) {
if (entities.hasOwnProperty(id)) {
result.push(entities[id])
}
}
return result
var result = [];
for (const id of idsFind) {
if (Reflect.has(entities, id)) {
result.push(entities[id])
}
}
return result
var result = [];
for (const id of idsFind) {
if (entities[id] != null) {
result.push(entities[id])
}
}
return result
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array.includes | |
Object.prototype.hasOwnProperty | |
object.hasOwnProperty | |
Reflect.has | |
object[field] |
Test name | Executions per second |
---|---|
array.includes | 1411.2 Ops/sec |
Object.prototype.hasOwnProperty | 65704.2 Ops/sec |
object.hasOwnProperty | 137055.4 Ops/sec |
Reflect.has | 66023.7 Ops/sec |
object[field] | 136134.0 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of four different approaches to check if an object has a property with a given key: Array.includes
, Object.prototype.hasOwnProperty
, object.hasOwnProperty
, and Reflect.has
. The test creates an array of 10,000 random IDs and an objects with corresponding values. It then iterates over the IDs and checks if each ID exists in the array or as a property of the object using each of the four approaches.
Approaches Compared
includes()
method on the array to check if a value is present.hasOwnProperty()
method on the object's prototype to check if a property exists.hasOwnProperty()
method directly on the object itself, bypassing the prototype chain.Reflect.has()
function to check if an object has a property with a given key.Pros and Cons of Each Approach
indexOf()
which can be slower.Reflect.has()
for better performance.Object.prototype.hasOwnProperty
as it doesn't traverse the prototype chain.Library and Syntax
None of the approaches use any external libraries or special JavaScript features beyond the basic syntax provided by the language. However, it's worth noting that Reflect.has()
is a modern feature introduced in ECMAScript 2020 (ES2020).
Other Considerations
When choosing between these approaches, consider the following:
Reflect.has()
might be the best choice.Array.includes
or Object.prototype.hasOwnProperty
might be a better option.Object.prototype.hasOwnProperty
or object.hasOwnProperty
might be a better choice as they are more explicit about their purpose.Alternatives
Other alternatives for checking if an object has a property with a given key include:
in
operator: if (key in obj) { ... }
has()
method on data structures like sets or mapsHowever, these alternatives might not be as widely supported or efficient as the approaches compared in this benchmark.