Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
just a test
(version: 0)
Comparing performance of:
reduce with shallow copy vs reduce without shallow copy vs foreach
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
reduce with shallow copy
const vertices = new Array(100).fill(0).map((_, i) => i); vertices.reduce((list, vertex) => ({ ...list, [vertex]: new Set() }), {});
reduce without shallow copy
const vertices = new Array(100).fill(0).map((_, i) => i); vertices.reduce((list, vertex) => { list[vertex] = new Set(); return list; }, {});
foreach
const vertices = new Array(100).fill(0).map((_, i) => i); let adjacencyList = {}; vertices.forEach(vertex => { adjacencyList[vertex] = new Set(); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce with shallow copy
reduce without shallow copy
foreach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce with shallow copy
5580.0 Ops/sec
reduce without shallow copy
146632.6 Ops/sec
foreach
125973.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in these benchmarks. **Benchmark Purpose** The provided JSON defines a benchmark that tests the performance of three different approaches to create an adjacency list from an array of vertices: 1. **Reduce with shallow copy**: This approach uses the `reduce()` method to iterate over the array and create a new object with nested sets. 2. **Reduce without shallow copy**: This approach is similar to the first one, but instead of creating a new set for each vertex, it creates a new set directly in the accumulator object. 3. **Foreach**: This approach uses a `forEach()` loop to iterate over the array and create an adjacency list by pushing sets into an object. **Options Compared** The benchmark is comparing the performance of these three approaches: * **Pros and Cons** + **Reduce with shallow copy**: Pros: concise and readable code. Cons: may involve more memory allocations and copies, leading to slower performance. + **Reduce without shallow copy**: Pros: avoids memory allocation and copying, potentially leading to faster performance. Cons: may result in less readable code. + **Foreach**: Pros: explicit loop control and no memory allocations or copies required. Cons: may be more verbose and less readable than the other two approaches. **Library Used** None of the benchmark definitions explicitly use a library. However, it's worth noting that `Set` objects are used in all three approaches, which is a built-in JavaScript data structure. **Special JS Features/Syntax** The benchmarks do not explicitly use any special JavaScript features or syntax. They focus on demonstrating the performance differences between three basic approaches to create an adjacency list. **Other Alternatives** If you're interested in exploring alternative approaches, here are some other ways to create an adjacency list: * **Using a loop**: You can also use a traditional `for` loop to iterate over the array and create the adjacency list. * **Using `Array.prototype.forEach()` with callback functions**: This approach is similar to the "foreach" benchmark definition but uses callback functions instead of nested sets. * **Using a library like `graphlib` or `NetworkX`**: These libraries provide optimized implementations for graph algorithms, including creating adjacency lists. In conclusion, these benchmarks demonstrate the performance differences between three basic approaches to create an adjacency list. By understanding the pros and cons of each approach, developers can choose the most efficient method for their specific use case.
Related benchmarks:
isEqual vs length 3
isEqual vs length 4
qqqqssss
Lodash union VS ES6 Set
compare boolean values v0.1
Comments
Confirm delete:
Do you really want to delete benchmark?