Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Array
(version: 1)
Comparing performance of:
Object vs Array find vs Array findIndex
Created:
9 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var codes = Array.from(Array(30000), () => `${Math.floor(Math.random() * 20)}`); var textDefinitionsArray = [ {code: '20', label: 'AAA', labelShort: 'AAA', description: undefined, body: undefined}, {code: '19', label: 'AA+', labelShort: 'AA+', description: undefined, body: undefined}, {code: '18', label: 'AA', labelShort: 'AA', description: undefined, body: undefined}, {code: '17', label: 'AA-', labelShort: 'AA-', description: undefined, body: undefined}, {code: '16', label: 'A+', labelShort: 'A+', description: undefined, body: undefined}, {code: '15', label: 'A', labelShort: 'A', description: undefined, body: undefined}, {code: '14', label: 'A-', labelShort: 'A-', description: undefined, body: undefined}, {code: '13', label: 'BBB+', labelShort: 'BBB+', description: undefined, body: undefined}, {code: '12', label: 'BBB', labelShort: 'BBB', description: undefined, body: undefined}, {code: '11', label: 'BBB-', labelShort: 'BBB-', description: undefined, body: undefined}, {code: '10', label: 'BB+', labelShort: 'BB+', description: undefined, body: undefined}, {code: '9', label: 'BB', labelShort: 'BB', description: undefined, body: undefined}, {code: '8', label: 'BB-', labelShort: 'BB-', description: undefined, body: undefined}, {code: '7', label: 'B+', labelShort: 'B+', description: undefined, body: undefined}, {code: '6', label: 'B', labelShort: 'B', description: undefined, body: undefined}, {code: '5', label: 'B-', labelShort: 'B-', description: undefined, body: undefined}, {code: '4', label: 'CCC+', labelShort: 'CCC+', description: undefined, body: undefined}, {code: '3', label: 'CCC', labelShort: 'CCC', description: undefined, body: undefined}, {code: '2', label: 'CCC-', labelShort: 'CCC-', description: undefined, body: undefined}, {code: '1', label: 'C-D', labelShort: 'C-D', description: undefined, body: undefined} ,]; var textDefinitionsObject = { "20": {label: 'AAA', labelShort: 'AAA', description: undefined, body: undefined}, "19": {label: 'AA+', labelShort: 'AA+', description: undefined, body: undefined}, "18": {label: 'AA', labelShort: 'AA', description: undefined, body: undefined}, "17": {label: 'AA-', labelShort: 'AA-', description: undefined, body: undefined}, "16": {label: 'A+', labelShort: 'A+', description: undefined, body: undefined}, "15": {label: 'A', labelShort: 'A', description: undefined, body: undefined}, "14": {label: 'A-', labelShort: 'A-', description: undefined, body: undefined}, "13": {label: 'BBB+', labelShort: 'BBB+', description: undefined, body: undefined}, "12": {label: 'BBB', labelShort: 'BBB', description: undefined, body: undefined}, "11": {label: 'BBB-', labelShort: 'BBB-', description: undefined, body: undefined}, "10": {label: 'BB+', labelShort: 'BB+', description: undefined, body: undefined}, "9": {label: 'BB', labelShort: 'BB', description: undefined, body: undefined}, "8": {label: 'BB-', labelShort: 'BB-', description: undefined, body: undefined}, "7": {label: 'B+', labelShort: 'B+', description: undefined, body: undefined}, "6": {label: 'B', labelShort: 'B', description: undefined, body: undefined}, "5": {label: 'B-', labelShort: 'B-', description: undefined, body: undefined}, "4": {label: 'CCC+', labelShort: 'CCC+', description: undefined, body: undefined}, "3": {label: 'CCC', labelShort: 'CCC', description: undefined, body: undefined}, "2": {label: 'CCC-', labelShort: 'CCC-', description: undefined, body: undefined}, "1": {label: 'C-D', labelShort: 'C-D', description: undefined, body: undefined} };
Tests:
Object
codes.forEach(code => textDefinitionsObject[code])
Array find
codes.forEach(code => textDefinitionsArray.find(d => d.code === code))
Array findIndex
codes.forEach(code => textDefinitionsArray.findIndex(d => d.code === code))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object
Array find
Array findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object
2647.4 Ops/sec
Array find
477.8 Ops/sec
Array findIndex
479.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
This benchmark titled "Object vs Array" compares the performance of accessing elements in two different data structures: an object (`textDefinitionsObject`) and an array (`textDefinitionsArray`). Both structures contain a set of key-value pairs with ratings as keys and their descriptive labels as values. ### 1. Options Compared: - **Object Access (`textDefinitionsObject`)**: - Accessing an item's value using a key directly from an object. - Benchmarked with the statement: `codes.forEach(code => textDefinitionsObject[code])`. - **Array Methods (`textDefinitionsArray`)**: - `find`: This method searches through the array to find the first object that matches a certain condition (where `code` is checked). - Benchmarked with: `codes.forEach(code => textDefinitionsArray.find(d => d.code === code))`. - `findIndex`: Similar to `find`, but it returns the index of the found item rather than the item itself. - Benchmarked with: `codes.forEach(code => textDefinitionsArray.findIndex(d => d.code === code))`. ### 2. Pros and Cons of Each Approach: #### **Object Access:** - **Pros:** - **Performance**: Generally faster for direct key access as it has constant time complexity (O(1)). - **Simplicity**: Easy to read and write when needing to retrieve data by known keys. - **Cons:** - **Flexibility**: Less flexible for operations that require searching based on conditions other than the key. #### **Array `find`:** - **Pros:** - **Versatility**: Can easily handle more complex conditions for data retrieval, making it more adaptable to varying data structures or requirements. - **Cons:** - **Performance**: Slower than object access because it has linear time complexity (O(n)), as it must traverse the array to find a match. #### **Array `findIndex`:** - **Pros:** - **Similar Benefits**: Like `find`, it provides flexibility for conditions without the need for an additional iteration. - **Cons:** - **Performance**: Also has a linear time complexity (O(n)), resulting in similar performance drawbacks as `find`. ### 3. Other Considerations: - The execution times show a significant difference in performance between these approaches. The benchmark results indicate that the object access performed at approximately **7318 executions per second**, while both the `find` and `findIndex` methods had dramatically lower performance, executing around **357 executions per second**. This illustrates the efficiency of accessing properties directly via objects compared to searching through an array. ### 4. Alternatives: Other options could include: - **Array Indexing**: Instead of using `find` or `findIndex`, if data was stored in a manner that supports indexing, direct indexing could be used for faster access. - **Maps**: Utilizing the `Map` object for key-value storage, offering similar performance benefits as objects, but with added functionality like maintaining insertion order. - **Dictionaries in Libraries**: Libraries like Lodash or Immutable.js provide optimized data structures for these types of operations and may offer performance benefits in larger datasets. Overall, the benchmark reveals that while arrays offer flexibility for operations based on conditions, object access remains superior in terms of performance when straightforward key-based access is required.
Related benchmarks:
Object in vs Object[]
teststest1
new array vs array length
find vs loop array test
setmptyarray
Array vs. object
=== vs ^
!!array.length vs array.length > 0
array.length vs array.length > 0 2
Comments
Confirm delete:
Do you really want to delete benchmark?