Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array find vs Set
(version: 0)
Test speed of object access by key vs array find to find object
Comparing performance of:
Object access vs Array find vs Set find
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from(Array(1000), (_,x) => ({key: x, value: x*10})); var objContainer = {}; var arrContainer = []; var setContainer = new Set(); for (let i = 100; i >= 0; i--) { const index = Math.floor(Math.random() * 1000); const item = items[index]; objContainer[item.key] = item; arrContainer.push(item) setContainer.add(item) }
Tests:
Object access
items.forEach(item => objContainer[item.value])
Array find
items.forEach(item => arrContainer.find(containerItem => containerItem.value === item.value))
Set find
items.forEach(item => setContainer.has(item.value))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object access
Array find
Set find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object access
1614070.5 Ops/sec
Array find
1569.1 Ops/sec
Set find
2381297.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. The provided JSON represents a JavaScript microbenchmark test case that measures the performance of three different approaches: object access by key, array find to find an object, and set find to check if an element exists in a set. **Options being compared:** 1. **Object access by key**: This approach uses the bracket notation (`objContainer[item.key]`) to directly access the value associated with a given key. 2. **Array find**: This approach uses the `find` method to search for an object within the array that matches a certain condition (in this case, `containerItem.value === item.value`). 3. **Set find**: This approach uses the `has` method to check if an element exists in the set. **Pros and cons of each approach:** * **Object access by key**: This approach is likely to be the fastest since it directly accesses the value associated with a given key, without the need for searching or iteration. However, this approach may require more memory allocation and deallocation than the other two options. * **Array find**: This approach can be slower since it requires iterating through the array to find the matching object. Additionally, if the array is not sorted or has a complex structure, the search might take longer. On the other hand, `find` method returns the first match it finds, which can be beneficial in certain scenarios. * **Set find**: This approach is likely to be faster than array find since sets are typically implemented as hash tables, allowing for constant-time lookups. However, set operations may require additional memory allocation and deallocation. **Library usage:** The benchmark uses the following libraries: * `Array.from`: A method that creates a new Array instance from an iterable or array-like object. * `Set`: A built-in JavaScript data structure that stores unique values. * `find` method: A method of arrays that returns the first element that satisfies the provided condition. **Special JS feature/syntax:** None mentioned in this benchmark. However, it's worth noting that some modern browsers and Node.js versions may have additional features or optimizations that could affect performance, such as: * `let const` declarations * Arrow functions * Rest parameters * Spread syntax These features can be used to optimize the code, but they are not explicitly mentioned in this benchmark. **Alternatives:** Other alternatives for accessing elements by key, searching arrays, or checking if an element exists in a set include: * Using `Object.keys()` and indexing into the resulting array * Using `indexOf()` method on arrays * Using `hasOwnProperty()` method on objects Keep in mind that these alternatives may have different performance characteristics compared to the approaches tested in this benchmark.
Related benchmarks:
Object key access vs array find
Object key access vs array find2
Object key access vs array find vs map
Object key access vs array find 100 items yeh ok
Comments
Confirm delete:
Do you really want to delete benchmark?