Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array find vs Map get fixed
(version: 0)
Comparing performance of:
Array vs Map
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = [{ id: "a", name: "asd", age: 12, isValid: false, }, { id: "b", name: "bsd", age: 12, isValid: false, }, { id: "c", name: "c", age: 12, isValid: false, }, { id: "d", name: "dsd", age: 12, isValid: false, }, { id: "e", name: "esd", age: 12, isValid: false, }, { id: "f", name: "fsd", age: 12, isValid: false, }, { id: "g", name: "gsd", age: 12, isValid: false, }, { id: "h", name: "hsd", age: 12, isValid: false, }, { id: "i", name: "isd", age: 12, isValid: false, },]; var map = new Map(); list.forEach((obj) => { map.set(obj.id, obj); }); var id = "e";
Tests:
Array
const foundObj = list.find((obj) => obj.id === id); foundObj.isValid = true;
Map
const foundObj2 = map.get(id); foundObj2.isValid = true;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array
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 what's being tested in this benchmark. **Overview** The benchmark compares the performance of two approaches to find an object in a list: 1. Using the `find` method on an array (`Array` test case) 2. Using the `get` method on a `Map` object (`Map` test case) **Options Compared** The benchmark compares two options: * **Array**: Using the `find` method to find an object in the list by comparing its `id` property with a given `id`. * **Map**: Using the `get` method to retrieve an object from a `Map` object, where the key is the `id` of the object. **Pros and Cons** Here are some pros and cons of each approach: **Array (find)** Pros: * Simple and intuitive * Wide support across different browsers and versions * Can be used for searching in various data structures, not just lists Cons: * May be slower than `Map` for large datasets due to the need to iterate over the entire list * May not perform well if the list is unsorted or has a large number of duplicate keys **Map (get)** Pros: * Much faster than `Array` for large datasets, as it uses hash-based lookup * Can reduce memory usage compared to storing all data in an array Cons: * Requires creating a `Map` object and populating it with data, which may add overhead * May not be suitable for searching unsorted or non-key-value data structures **Library Used** In the `Script Preparation Code`, a `Map` library is created using `var map = new Map();`. The `list.forEach` method is used to populate the map with objects. **Special JS Feature/Syntax** None mentioned in this benchmark. However, it's worth noting that the use of `let const` (e.g., `const foundObj`) and template literals (`\r\n\tid: "a"\r\n`) are modern JavaScript features. **Other Alternatives** For searching in lists: * Using a custom function to iterate over the list and check each element's properties * Using a library like Lodash or Underscore.js for array utilities For searching in maps: * Using a custom function to iterate over the map and access each value's properties * Using a library like Lodash or Underscore.js for object utilities
Related benchmarks:
Search: Array to Map and find vs Array.find
array find vs object key search
filter + map vs flatMap
Search: Array to Map and find vs Array.find685000
Search: Array to Map and find vs Array.find 2
Comments
Confirm delete:
Do you really want to delete benchmark?