Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
my testtestets
(version: 0)
Comparing performance of:
find vs map
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandomElement(id) { return { id, a: Math.random(), b: Math.random(), c: Math.random(), } } function getArray(length) { const result = []; for (let i = 0; i < length; i++) { result.push(getRandomElement(i)) } return result; } function arrayToMap(array) { return new Map(array.map(el => [el.id, el])); } function getRandomInt(max) { return Math.floor(Math.random() * max); } array_small = getArray(10000); map_small = arrayToMap(array_small);
Tests:
find
for (let i = 0; i < 1000; i++) { const target = getRandomInt(9999); array_small.find(el => el.id === target); }
map
for (let i = 0; i < 1000; i++) { const target = getRandomInt(9999); map_small.get(target); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find
map
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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find
173.9 Ops/sec
map
3045.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested, compared, and considered. **Benchmark Overview** The benchmark is testing two different approaches to searching for an element in an array: using the `find` method versus using a Map data structure. **Script Preparation Code** The script preparation code defines several functions: * `getRandomElement`: returns a random object with `id`, `a`, `b`, and `c` properties. * `getArray`: generates an array of 10,000 elements by pushing `getRandomElement` objects into it. * `arrayToMap`: converts the generated array into a Map data structure. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark is focused solely on JavaScript performance and does not account for any DOM-related overhead. **Benchmark Definition** The benchmark definition consists of two test cases: 1. **find**: searches for an element in the `array_small` array using the `find` method. 2. **map**: searches for an element in the `map_small` Map data structure using the `get` method. **Comparison** The benchmark is comparing the performance of these two approaches: * Using the `find` method on a large array * Using a Map data structure to store and search elements **Pros and Cons** **Find Method:** Pros: * Easy to implement and understand * Well-supported by most JavaScript engines Cons: * Has to iterate over the entire array, which can be slow for large datasets * May not perform well if the array is not sorted or has duplicate values **Map Data Structure:** Pros: * Fast lookups, with an average time complexity of O(1) * Can handle large datasets and duplicates without performance issues Cons: * Requires more memory to store the Map data structure * Can be more complex to implement and understand **Library and Purpose** The `Array.prototype.find` method is a built-in JavaScript method that searches for the first element in an array that satisfies the provided condition. The `Map` data structure is also a built-in JavaScript data structure that stores key-value pairs. **Special JS Feature or Syntax** There are no special JS features or syntax used in this benchmark, apart from the use of the `const` and `let` keywords for variable declarations. **Other Considerations** * Memory usage: The Map data structure may consume more memory than the array approach, especially for large datasets. * Cache locality: The performance of the `find` method may be affected by cache locality, as it has to iterate over the entire array. In contrast, the Map data structure can take advantage of cache locality, making lookups faster. **Alternatives** Other alternatives for searching elements in an array or Map data structure include: * Using a sorted array and binary search * Using a tree-based data structure, such as a trie or a quadtree * Using a custom implementation, such as a hash table or a Bloom filter Keep in mind that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
Array.find vs. Map.getss
Array.find vs. Map.get fork
Array.find vs. Map.get 2
Array.find vs. Map.get 300
Comments
Confirm delete:
Do you really want to delete benchmark?