Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sorting
(version: 0)
map & find with O(n^2) vs 2 iterations
Comparing performance of:
map & find vs lookup map using obj vs lookup map using map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
ids = [...Array(1000)].map(() => window.crypto.randomUUID()); shuffledData = ids.sort(() => 0.5 - Math.random()).map(id => ({id, value: window.crypto.randomUUID()}));
Tests:
map & find
const sortedData = ids.map(id => shuffledData.find(item => item.id === id));
lookup map using obj
const map = {}; shuffledData.forEach(item => map[item.id] = item); const sortedData = ids.map(id => map[id]);
lookup map using map
const map = new Map(); shuffledData.forEach(item => map.set(item.id, item)); const sortedData = ids.map(id => map.get(id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map & find
lookup map using obj
lookup map using map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map & find
193.3 Ops/sec
lookup map using obj
13903.1 Ops/sec
lookup map using map
15215.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined by a JSON object that contains: * `Name`: The name of the benchmark, which is "sorting". * `Description`: A brief description of the benchmark, which compares three different approaches for sorting data. * `Script Preparation Code`: A code snippet that creates an array of 1000 random IDs and generates an equal number of objects with those IDs. The objects are shuffled randomly using the `sort()` method. * `Html Preparation Code`: An empty string, indicating that no HTML preparation is needed. **Individual Test Cases** The benchmark consists of three test cases: 1. **"map & find"`: This test case creates a sorted array by mapping over the shuffled data and finding each element in the original array using the `find()` method. 2. **"lookup map using obj"`: This test case creates an object with the same structure as the shuffled data, where each key is an ID and the value is the corresponding object. It then maps over the sorted IDs to retrieve the corresponding objects from the object. 3. **"lookup map using map"`: This test case creates a Map object with the same structure as the shuffled data, where each key is an ID and the value is the corresponding object. It then maps over the sorted IDs to retrieve the corresponding objects from the Map. **Comparison of Approaches** The three approaches are being compared in terms of their execution speed: * **"map & find"`**: This approach uses the `find()` method, which has a time complexity of O(n) for arrays, but is more efficient than iterating over the array using a loop. * **"lookup map using obj"`: This approach uses an object to store the data and iterates over the sorted IDs to retrieve the corresponding objects. The lookup operation in this approach has a time complexity of O(1), making it potentially faster than the other approaches. * **"lookup map using map"`: This approach uses a Map object to store the data, which provides fast lookups with an average time complexity of O(1). This approach is likely to be the fastest. **Pros and Cons** Here are some pros and cons for each approach: * **"map & find"`**: Pros - simple to implement, easy to understand. Cons - may not be as efficient as other approaches due to the use of `find()` method. * **"lookup map using obj"`: Pros - fast lookups, easy to implement. Cons - requires an object with the same structure as the shuffled data, which may require additional memory allocation. * **"lookup map using map"`: Pros - fast lookups, efficient memory usage. Cons - requires creating a Map object, which may have additional overhead. **Library Used** None of the benchmark cases use any external libraries or frameworks that are not part of the JavaScript standard library. **Special JS Features or Syntax** There is no special JS feature or syntax used in these benchmark cases. The code only uses basic JavaScript syntax and features, such as arrays, objects, loops, and conditional statements. **Other Alternatives** If you're looking for alternative approaches to sorting data in JavaScript, here are a few options: * Using the `sort()` method with a custom compare function. * Using a priority queue or a binary search tree to sort data efficiently. * Using a streaming algorithm that processes data in chunks rather than loading it all into memory. Keep in mind that the choice of sorting algorithm depends on the specific requirements and constraints of your application.
Related benchmarks:
Already sorted versus random
.find() vs direct access in object by id
shuffle array [dsng-manscaped]
Math.random vs Crypto.getRandomValues for 10k values
Comments
Confirm delete:
Do you really want to delete benchmark?