Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Array lookups
(version: 0)
Comparing performance of:
Array lookup vs Obj lookuo
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 = 1000, jane;
Tests:
Array lookup
for (i = 0; i < count; i++) { janeIndex = reportsArr.findIndex(el => el.id === 'jane'); }
Obj lookuo
for (i = 0; i < count; i++) { janeIndex = reportsObj['jane']; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array lookup
Obj lookuo
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 JSON benchmark and explain what is being tested, compared, and other considerations. **Benchmark Definition** The benchmark definition is represented by two test cases: 1. **Array Lookup**: This test case uses a `for` loop to iterate `count` number of times (1000). Inside the loop, it finds the index of an element in the `reportsArr` array that matches the string `'jane'`. The code `janeIndex = reportsArr.findIndex(el => el.id === 'jane');` is used for this purpose. 2. **Object Lookup**: This test case also uses a `for` loop to iterate `count` number of times (1000). Inside the loop, it finds the value associated with the key `'jane'` in the `reportsObj` object using the syntax `reportsObj['jane']`. **Comparison** The two test cases are compared in terms of execution time. The one that executes faster will be reported as the winner. **Pros and Cons** * **Array Lookup**: This approach is generally faster for large arrays because JavaScript's array methods, such as `findIndex`, have optimized implementations. + Pros: Fast lookup, especially for large datasets. + Cons: May require more memory to store the entire array, and the indexing operation can be slower if the array is too large. * **Object Lookup**: This approach uses a dictionary-like data structure (object) and relies on property access syntax (`reportsObj['jane']`). + Pros: Can be faster for small to medium-sized datasets, as object lookups are often more cache-friendly than array lookups. + Cons: May not scale well for very large datasets, and the lookup can be slower if the object is too large. **Library/Function** There is no specific library mentioned in the benchmark definition. However, `findIndex` is a built-in JavaScript method used in both test cases. **Special JS Feature/Syntax** The benchmark uses the following special syntax: * `template literals`: The string concatenation in the `Script Preparation Code`, specifically: `"var reportsArr = [...]; var reportsObj = {...};"`. This syntax allows for more readable and efficient string construction. * **Arrow functions**: Although not explicitly used, arrow functions are implied by the syntax `el => el.id === 'jane'` in the array lookup test case. Arrow functions provide a concise way to define small, single-expression functions. **Alternatives** Other alternatives could include: * Using other data structures like `Map` or `Set`, which might offer different performance characteristics. * Implementing custom lookup algorithms or data structures. * Using libraries like Lodash, which provides various utility functions for working with arrays and objects. Keep in mind that the optimal approach depends on the specific use case, dataset size, and performance requirements.
Related benchmarks:
filter vs id lookup
typeof first or second
Array.find vs map to object then lookup.
array includes vs object key lookup, large arrays
indexOf vs multiple === vs object selection
Comments
Confirm delete:
Do you really want to delete benchmark?