Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test Array manipulations
(version: 0)
Comparing performance of:
Array vs Map
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arrayStorage = new Array(5000).fill(0).map((v, i) => ({id: i, value: i})) var mapStorage = new Map(arrayStorage.map((entry) => [entry.id, entry])) var newItem = {id: 25, value: 222}
Tests:
Array
var index = arrayStorage.findIndex((item) => item.id === newItem.id) arrayStorage = [...arrayStorage.slice(0, index), newItem, ...arrayStorage.slice(index+1)]
Map
mapStorage.delete(newItem.id) arrayStorage = Array.from(mapStorage.values())
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 is being tested in this benchmark. **Overview** The test measures the performance of two JavaScript operations on arrays and maps: finding an element by id using `findIndex`, adding an element to the array, and deleting an element from the map. The goal is to compare the execution speed of these two approaches. **Options Compared** There are two options being compared: 1. **Array**: Using `arrayStorage` (a dense array) to find an element by id using `findIndex`, add a new element to the array, and then update the array. 2. **Map**: Using `mapStorage` (a map) to find an element by id using `delete` on the map's values, convert the map's values to an array, and then update the array. **Pros and Cons** 1. **Array** * Pros: + Familiar data structure for many developers + Fast lookup times for arrays (O(n) is rare) * Cons: + Can lead to slow performance due to unnecessary copying of elements + May require additional memory allocations 2. **Map** * Pros: + Efficient key-value storage and lookup (O(1) on average) + Reduces memory allocations compared to arrays * Cons: + Requires JavaScript objects to be converted to arrays or iterables, which can incur overhead **Library and Syntax** The test case uses the `Map` data structure from the ECMAScript standard library. The `Map` object provides an efficient way to store key-value pairs, allowing for fast lookups using the `has` method or the `delete` method. No special JavaScript features or syntax are used in this benchmark. **Considerations** * The test measures the execution speed of both approaches, but it's essential to consider other factors like memory usage and code complexity when choosing between arrays and maps. * For certain use cases, such as caching or storing large amounts of data, `Map` might be a better choice due to its efficiency. However, for smaller datasets or simple lookups, an array might be sufficient. **Other Alternatives** If you're considering alternative approaches, here are a few options: 1. **Using other data structures**: Depending on your specific use case, you might consider using other data structures like `Set`, `SortedSet`, or even custom implementations (e.g., binary search trees). 2. **Native methods**: Depending on the browser or environment, you might have access to native methods for searching and manipulating arrays or maps. 3. **Optimized libraries**: If performance is critical, you can consider using optimized JavaScript libraries that provide efficient data structures and algorithms. Keep in mind that each approach has its pros and cons, and the best choice depends on your specific use case and requirements.
Related benchmarks:
Array range generating
Object spread vs New map
Object spread vs New map entries
Array.fill vs Array.from with dyamnic data
Array.from vs new Array using index value
Comments
Confirm delete:
Do you really want to delete benchmark?