Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Array.findIndex lookups
(version: 0)
Comparing performance of:
Array.findIndex lookup vs Obj lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var reportsArr = [{ id: `john`, text: `one` }, { id: `jane`, text: `two` }, { id: `bob`, text: `three` }] var reportsObj = { ['john']: { text: `one` }, [`jane`]: { text: `two` }, [`bob`]: { text: `three` } } var i = 0, count = 10000, jane;
Tests:
Array.findIndex lookup
for (i = 0; i < count; i++) { janeIndex = reportsArr.findIndex(el => el.id === 'jane'); jane = reportsArr[janeIndex] }
Obj lookup
for (i = 0; i < count; i++) { jane = reportsObj['jane']; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.findIndex lookup
Obj lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition Json** The JSON represents a JavaScript microbenchmark named "Object vs Array.findIndex lookups". The benchmark has two test cases: 1. **Array Finding Index** 2. **Object Lookup** The script preparation code initializes two arrays, `reportsArr` and `reportsObj`, which contain similar data but in different formats (arrays vs objects). The `i` variable is initialized to 0, and the count variable is set to 10000. **Options Compared** The benchmark compares two approaches: 1. **Array Finding Index**: Using the `findIndex()` method on an array to find the index of a specific element. 2. **Object Lookup**: Directly accessing an object property using square brackets (`[]`) to get a value. **Pros and Cons** ### Array Finding Index Pros: * More modern approach, as it was introduced in ECMAScript 2015 (ES6). * Can be more efficient for large arrays, as it uses a binary search algorithm. * Does not require the use of square brackets (`[]`) to access properties. Cons: * Requires an additional step to get the actual value from the array using indexing (`reportsArr[janeIndex]`). ### Object Lookup Pros: * More traditional approach, widely supported in older browsers and environments. * Does not require the use of square brackets (`[]`) or indexing (`reportsObj['jane']`). * Can be more readable for those familiar with object notation. Cons: * Less efficient than `findIndex()` for large arrays, as it uses a linear search algorithm. * Requires access to the property using square brackets (`[]`). **Library and Purpose** In the script preparation code, the library used is not explicitly mentioned. However, based on the context, it appears that the benchmark assumes the use of JavaScript's built-in `findIndex()` method, which does not require any external libraries. **Special JS Feature or Syntax** No special JS feature or syntax is used in this benchmark. It only uses standard JavaScript syntax and features. **Other Alternatives** If you wanted to rewrite these tests using alternative approaches, here are a few options: 1. **Array.prototype.indexOf()**: Instead of `findIndex()`, you could use the `indexOf()` method on the array, which is similar but does not have the same benefits for large arrays. 2. **Object.keys() and Array.prototype.forEach()**: You could access object properties using `object.keys()` and then iterate over the resulting array using `Array.prototype.forEach()` to get a value. 3. **Destructuring assignment**: If you're familiar with modern JavaScript syntax, you could use destructuring assignment to directly extract the desired property from the object without needing square brackets (`[]`). However, these alternatives would likely not be as efficient or readable as the original `findIndex()` and object lookup approaches.
Related benchmarks:
filter vs id lookup
IndexOf vs Includes vs find
find in array of objects by field 1002
find vs findIndex (Array prototype methods) - using objects
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?