Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs spread in building graph from edges
(version: 0)
Comparing performance of:
push vs spread
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
push
const edges = [ ['w', 'x'], ['x', 'y'], ['z', 'y'], ['z', 'v'], ['w', 'v'] ]; const buildGraphPush = (edges) => { let graph = {}; for (let e of edges) { const [a, b] = e; if (!(a in graph)) graph[a] = []; if (!(b in graph)) graph[b] = []; graph[a].push(b); graph[b].push(a); } return graph; }; buildGraphPush(edges);
spread
const edges = [ ['w', 'x'], ['x', 'y'], ['z', 'y'], ['z', 'v'], ['w', 'v'] ]; const buildGraphSpread = (edges) => { let graph = {}; for (let e of edges) { const [a, b] = e; graph[a] = [...(graph[a] || []), b]; graph[b] = [...(graph[b] || []), a]; } return graph; }; buildGraphSpread(edges);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
push
spread
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push
4221480.5 Ops/sec
spread
3196669.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested in each test case. **Benchmark Definition** The benchmark definition is a set of instructions that describes how to run the test. In this case, it's not present in the JSON data, so we'll move on to the individual test cases. **Individual Test Cases** There are two test cases: 1. **Push** This test case defines an array `edges` containing pairs of node names (`w`, `x`, `y`, `z`, and `v`). The benchmark measures how long it takes to build a graph using the `push` method. The graph building function `buildGraphPush` iterates over the edges, adding each node as a key in the graph object if it doesn't exist. For each edge, it pushes the second node onto the first node's array. **Spread** This test case is similar to the previous one, but instead of using `push`, it uses the spread operator (`...`) to add nodes to the graph. The function `buildGraphSpread` iterates over the edges, adding each node as a key in the graph object. For each edge, it adds the second node to the first node's array using the spread operator. **Options Compared** In this benchmark, we have two options being compared: 1. **Push**: uses the `push` method to add nodes to the graph. 2. **Spread**: uses the spread operator (`...`) to add nodes to the graph. **Pros and Cons of Each Approach** 1. **Push**: * Pros: fast and efficient, as it only adds a single node to the existing array. * Cons: can be slower for large graphs due to the overhead of creating a new array. 2. **Spread**: * Pros: creates a new array with all nodes, which can lead to better performance in some cases (e.g., when nodes are frequently added or removed). * Cons: slower than `push` due to the overhead of creating a new array. **Library Used** In neither test case is there any explicit library usage mentioned. However, it's likely that JavaScript's built-in functions and data structures are being used. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes explicitly used in these benchmark cases. However, the use of the spread operator (`...`) might be considered a modern JavaScript feature. **Alternatives** For building graphs, there are other approaches that might be relevant: 1. **Adjacency Matrix**: a matrix where each cell [i][j] represents the weight of the edge between nodes i and j. 2. **Union-Find Algorithm**: an algorithm for finding disjoint sets in a graph. However, these alternatives might not be directly applicable to this specific benchmark case, which is focused on comparing the performance of two specific approaches (push vs spread) for building a graph from edges.
Related benchmarks:
spread operator vs push test - correct
Pushing items via Array.push vs. Spread Operator
spread vs push large
Spread vs Push -
JS array spread operator vs push
Comments
Confirm delete:
Do you really want to delete benchmark?