let arr = {}
for (let i=0; i<500; i++) {
arr = { arr, [+i]: { id: +i, foo: 'bar'}}
}
const test = arr[40]
const arr = []
for (let i=0; i<500; i++) {
arr.push({ id: +i, foo: 'bar'})
}
const test = arr.find(x => x.id === 40)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexation | |
arr |
Test name | Executions per second |
---|---|
indexation | 24273.1 Ops/sec |
arr | 682542.1 Ops/sec |
Benchmark Explanation
MeasureThat.net is designed to compare the performance of two approaches: accessing an array using its index or using the Array.prototype.indexOf() method.
The benchmark is testing which approach is faster for two specific test cases:
find()
method is used to find the object with a specific id (in this case, 40).Options Compared
The two approaches being compared are:
Library and Special JS Features
In this benchmark, there is no library being used. However, the find()
method is being compared to direct array access using an index.
Other Considerations
When deciding between these two approaches, consider the following factors:
Alternatives
If you're working with arrays and need to decide whether to use direct access or indexOf()
, here are some alternatives to consider:
map()
instead of pushing: If you need to perform an operation on each element in the array, using map()
can be more efficient than pushing elements onto the end of the array.forEach()
instead of accessing the array directly: Similar to map()
, using forEach()
can be faster than accessing the array directly for iterative operations.Keep in mind that these alternatives will depend on the specific use case and requirements of your application.