Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object new
(version: 0)
my own
Comparing performance of:
GetFromArray vs GetFromMap
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [{ code: "a", status: "1" }, { code: "b", status: "2" }, { code: "c", status: "3" }, { code: "d", status: "4" }, { code: "e", status: "5" }, { code: "f", status: "6" }, { code: "g", status: "7" }, { code: "h", status: "8" } ] var arrForMap = [ ["a", "1"], ["b", "2"], ["c", "3"], ["d", "4"], ["e", "5"], ["f", "6"], ["g", "7"], ["h", "8"], ]
Tests:
GetFromArray
for (i = 0; i < 10000; i++) { a = arr.find((e) => e.code === "h").status; }
GetFromMap
const newMap = new Map(arrForMap); for (i = 0; i < 10000; i++) { a = newMap.get("h"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
GetFromArray
GetFromMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
GetFromArray
867.4 Ops/sec
GetFromMap
880.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark compares the performance of two approaches to retrieve data from arrays and maps in JavaScript: 1. `for` loop with `arr.find()` 2. Creating a new map from an array and using it with `newMap.get()` **Options Comparison** Two options are compared: A) Using `arr.find()` with a callback function to search for an element in the array. B) Creating a new map from an array of key-value pairs using `Array.prototype.map()` and then using `map.get()` to retrieve values from the map. **Pros and Cons of Each Approach** ### A) `for` loop with `arr.find()` Pros: * Simple and straightforward implementation. * No additional memory allocation or data structure creation required. Cons: * Iterates over the entire array, which can be slow for large datasets. * Returns an iterator object that must be traversed to get the result. ### B) Creating a new map from an array and using `newMap.get()` Pros: * Can take advantage of JavaScript's built-in hash table data structure (maps), which provides faster lookup times. * Reduces iteration over the entire dataset, making it more suitable for large datasets. Cons: * Requires additional memory allocation to create a new map object. * May incur overhead due to the creation and garbage collection of the map object. **Library and Special JS Feature** None mentioned in the provided benchmark definition. However, `Array.prototype.map()` is used to convert an array to a map, which is a built-in JavaScript feature. **Other Considerations** When working with large datasets or performance-critical applications, it's essential to consider factors such as: * Memory allocation and garbage collection * Data structure choice (e.g., arrays vs. maps) * Iteration algorithms and their efficiency **Alternatives** Other approaches to retrieve data from arrays and maps include: A) Using `Array.prototype.indexOf()` or `indexOf()` method, which returns the index of the first occurrence of a specified element. B) Implementing custom search algorithms using bitwise operations or other techniques. C) Utilizing specialized libraries or frameworks that provide optimized data retrieval mechanisms. Keep in mind that these alternatives may have varying trade-offs in terms of performance, memory usage, and implementation complexity.
Related benchmarks:
for vs map
red test
Tagsads
.flatMap vs .map.flat
flatMap + flatMap vs flat(2)+map
Comments
Confirm delete:
Do you really want to delete benchmark?