var list = []
var map = new Map();
var SIZE = 1000;
for (let i = 0; i < SIZE; i++) {
const item = { id: "ID-" + i, value: i};
list.push(item)
map.set(item.id, item)
}
for (var i = 0; i < SIZE; i++) {
const result = list.find((item) => item.id === "ID-" + i);
}
for (let i = 0; i < SIZE; i++) {
const result = map.get("ID-" + i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.find performance | |
Map.get performance |
Test name | Executions per second |
---|---|
Array.find performance | 123.4 Ops/sec |
Map.get performance | 6965.2 Ops/sec |
Let's break down the provided JSON and explain what is being tested.
Benchmark Definition
The benchmark is comparing two approaches: Array.find
and Map.get
. The script preparation code creates an array of 1000 objects and a Map with the same data. This allows us to test these two methods on the same dataset.
Options Compared
There are two options being compared:
id
property.Pros and Cons
Here are some pros and cons for each approach:
Map.get
if you need to perform multiple lookups on the same array.Array.find
.Library and Purpose
The library used in this benchmark is not explicitly mentioned in the provided JSON. However, it's likely that the JavaScript engine being tested is using a internal implementation of these methods. If you were to write your own implementation of these methods, you would need to consider factors such as cache coherence, data locality, and branch prediction.
Special JS Feature/Syntax
There doesn't appear to be any special JavaScript feature or syntax used in this benchmark.
Other Alternatives
If you wanted to test other approaches, you might consider:
Array.find
and Map.get
calls, such as filtering or sorting the array.Benchmark Preparation Code Explanation
The script preparation code creates an empty array list
and a new Map map
. It then sets the size of the dataset to 1000 using the SIZE
variable. The loop creates 1000 objects with id
properties that match the index value, and adds them to both the array and the map.
The HTML preparation code is not provided in the JSON, but it's likely that there would be some HTML elements set up to capture performance data from the browser.