Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
overwrite new Map() vs findIndex
(version: 0)
Benchmark.js
Comparing performance of:
new Map() vs findIndex
Created:
2 years ago
by:
Guest
Go to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
new Map()
let myMap = new Map(); for (let index = 0; index < 100; index++) { const element = { _id : index, name : _.times(20, () => _.random(35).toString(36)).join('') } myMap.set(index, element); } if(myMap.has(65)) { let foundMap = myMap.get(65); foundMap.name = 'HelloMyNewName'; }
findIndex
let array = []; for (let index = 0; index < 100; index++) { const element = { _id : index, name : _.times(20, () => _.random(35).toString(36)).join('') } array.push(element); } let foundIndex = array.findIndex((array1) => array1._id); if(foundIndex !== -1) { array[foundIndex].name = 'HelloMyNewName'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Map()
findIndex
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
new Map()
20K/s
findIndex
19K/s
View exact numbers
Test name
Executions per second
✓
new Map()
20,130 Ops/sec
findIndex
19,277 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested in each test case. **Benchmark Definition** The `Name` field indicates that this benchmark is comparing two approaches: creating a new Map (`new Map()`) versus using the `findIndex` method to search for an element in an array. **Options Compared** * **Create a new Map**: This approach creates a new, empty Map object and sets its elements using the `set` method. * **Use findIndex to search for an element in an array**: This approach creates an empty array, pushes 100 elements into it, and then uses the `findIndex` method to find the index of a specific element. If found, it updates the value at that index. **Pros and Cons** Creating a new Map: Pros: * Maps are designed for storing key-value pairs and provide efficient lookups, insertions, and deletions. * Maps can grow dynamically as elements are added. Cons: * Creating a new Map object involves overhead in terms of memory allocation and garbage collection. * If the map is not properly managed (e.g., not cleared or resized), it can lead to performance issues. Using findIndex to search for an element in an array: Pros: * Arrays are optimized for random access and have fast lookup times. * The `findIndex` method only scans through the array until it finds the specified element, making it efficient for large datasets. Cons: * Arrays require indexing into memory, which can be slower than Maps' lookup mechanism. * If the array is not properly managed (e.g., not sorted or indexed), it can lead to slower performance. **Library and Purpose** The `lodash` library is used in both test cases. Specifically, it provides the `times` function, which generates an array of a specified length by executing a callback function repeatedly. In this case, the `times` function is used to generate random strings for each element in the array or map. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes mentioned in the provided data. However, it's worth noting that the use of `_.random(35).toString(36)` suggests that the benchmark might be running on a browser with a JavaScript engine that supports the `toString` method with radix 36 (hexadecimal). **Alternatives** Other alternatives to create maps or search for elements in arrays could include: * Using an object instead of a map * Using a data structure like a hash table or trie * Implementing your own binary search algorithm * Utilizing third-party libraries like `fast-async` or `jsperf` Keep in mind that the choice of data structure and algorithm depends on the specific requirements and constraints of the project.
Related benchmarks
Array.prototype.map vs Lodash.map on large data
Ladash map versus ECMA6 map
indexOf vs Lodash indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?