Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create a map once then query it, or query each value.
(version: 0)
when receiving a large result, should you query said result and reused the cache value, or should you search the value each time.
Comparing performance of:
Search the array. vs Make a map then search.
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = { id: i++ };
Tests:
Search the array.
let j = 0; while (j <= 1E5) { const item = arr.find(e => e.id == j++); }
Make a map then search.
let myMap = new Map(arr.map(e => ([e.id, e]))); let j = 0; while (j <= 1E5) { const item = myMap.get(j++); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Search the array.
Make a map then search.
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Search the array.
2563.3 Ops/sec
Make a map then search.
106.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested in the benchmark. **Benchmark Definition** The first part of the JSON represents the overall benchmark definition. Here, we have: * **Name**: A descriptive name for the benchmark. * **Description**: A brief explanation of the purpose of the benchmark. In this case, it's about comparing two approaches: searching through a large array directly or using a map data structure to store and retrieve values efficiently. **Script Preparation Code** The script preparation code is used to set up the environment before running each test case. Here, we have: * **Creating an empty array**: `var arr = [];` * **Initializing a counter variable**: `var i = 0;` * **Populating the array with data**: The `while` loop populates the array with objects, where each object has an `id` property. **Html Preparation Code** There is no HTML preparation code provided in this case. Now, let's move on to the individual test cases: **Test Cases** We have two test cases: 1. **Search the array**: This test case uses a simple `while` loop to iterate through the array and find an item based on its `id` property. 2. **Make a map then search**: This test case creates a map data structure from the array using the `Map` constructor, which maps each element's `id` property to itself. **Libraries and Features** In the "Make a map then search" test case, we use the built-in `Map` object in JavaScript. The purpose of this library is to provide a data structure that stores key-value pairs, allowing for efficient lookups by key. As for special JavaScript features or syntax, there are none mentioned in this benchmark. **Approaches** So, what approaches are being compared? 1. **Direct array search**: This approach involves searching through the entire array to find an item based on its `id` property. 2. **Using a map data structure**: This approach creates a map from the array and uses it to look up values by their `id` property. **Pros and Cons** Here are some pros and cons of each approach: * **Direct array search**: + Pros: Simple, easy to implement. + Cons: Can be slow for large datasets due to linear search. * **Using a map data structure**: + Pros: Efficient lookups, especially for large datasets or frequent searches. + Cons: Requires additional memory and computational resources to create the map. **Other Considerations** When designing benchmarks like this one, it's essential to consider factors such as: * **Dataset size**: The larger the dataset, the more significant the differences between approaches will be. * **Search patterns**: Are the searches uniform or will they follow a particular pattern? This can affect performance. * **Hardware and software characteristics**: Different browsers, devices, or environments might exhibit different performance characteristics. **Alternatives** If you wanted to create alternative test cases, you could consider: * Using other data structures like `Set` or `TreeMap`. * Introducing noise in the search pattern (e.g., randomizing the order of elements). * Varying the dataset size and composition. * Adding additional factors that might impact performance, such as sorting or filtering. By exploring these alternatives, you can gain a deeper understanding of how different approaches affect performance under various scenarios.
Related benchmarks:
For loop map vs map builtin for 10000000 elements
For loop map vs map builtin for 100000 elements
Array find by id or indexOf
create map first and find many vs array find many
Comments
Confirm delete:
Do you really want to delete benchmark?