Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs Object vs Map find
(version: 0)
Benchmarks finding of a pin (value) in a haystack (array, object or map)
Comparing performance of:
Find in Array vs Find in Object vs Find in Map
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const entries = Array(500000).fill('').map((_, index) => [`entry ${index}`, 'value']) window.array = [...entries] window.object = Object.fromEntries(entries) window.map = new Map() entries.forEach(([key, value]) => { window.map.set(key, value) }) window.keyToFind = 'entry 4000'
Tests:
Find in Array
window.array.find(([key]) => key === window.keyToFind)
Find in Object
window.object[window.keyToFind]
Find in Map
window.map.get(window.keyToFind)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Find in Array
Find in Object
Find in Map
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of three different data structures: arrays, objects, and maps. The test case uses a single value (`window.keyToFind`) to search for in each data structure. The goal is to find this value as quickly as possible. **Script Preparation Code** Before running the benchmark, the script preparation code creates: 1. A large array (`window.array`) with 500,000 elements. 2. An object from an array of key-value pairs (`window.object`). 3. A map from the same array of key-value pairs (`window.map`). **Html Preparation Code** Since this is a JavaScript benchmark, there's no HTML preparation code. **Individual Test Cases** The test cases are: 1. **Find in Array**: Use the `find()` method to search for the value in the array. 2. **Find in Object**: Access the value directly using bracket notation (`window.object[window.keyToFind]`). 3. **Find in Map**: Use the `get()` method to retrieve the value from the map. **Pros and Cons of Each Approach** Here's a brief summary of each approach: 1. **Array Find**: Using `find()`, you need to provide a callback function that checks if the value exists in the array. This can be slower than direct access because it involves a search. * Pros: Portable across browsers, easy to implement. * Cons: May incur additional overhead due to the callback function. 2. **Object Access**: Directly accessing the value using bracket notation is fast and efficient. * Pros: Fastest approach, no additional overhead. * Cons: Not portable across all browsers (some older versions may not support it). 3. **Map Get**: Using `get()` is similar to object access but with the added benefit of being more memory-efficient. * Pros: More efficient than array find, still fast for direct access. * Cons: May incur additional overhead due to map operations. **Library and Purpose** In this benchmark, there are no external libraries used. However, the `Object.fromEntries()` method is a modern JavaScript feature introduced in ES6, which creates an object from an array of key-value pairs. **Special JS Feature or Syntax** There's one notable aspect: **template literals** (`const entries = Array(500000).fill('').map((_, index) => [`entry ${index}`, 'value'])`). Template literals are a feature introduced in ES6, allowing you to embed expressions inside string literals. This is used to create an array of key-value pairs with dynamic keys. **Other Alternatives** If you're interested in exploring alternative approaches or optimizations, here are some options: * Use `forEach()` and `includes()` instead of `find()`. * Optimize the search algorithm for specific use cases (e.g., sorted arrays). * Use a more efficient data structure, like a `Set` or a trie. * Explore other benchmarking tools and frameworks that offer more advanced features and customization options.
Related benchmarks:
map vs forEach Chris v2b
is find faster than forEach?
Array.forEach vs Object.keys().forEach
Array.from() vs new Array() with index
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?