Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread or push 1
(version: 0)
Comparing performance of:
spread vs push
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
spread
const nodes = [ {id: 1, graphKey: 'key1'}, {id: 9, graphKey: 'key9'}, {id: 3, graphKey: 'key3'}, {id: 4, graphKey: 'key4'}, {id: 5, graphKey: 'key5'}, ] const selected = [1,2,3,4] const selectedNodeIds = selected.reduce((result, currentSelected) => { const node = nodes.find((n) => n.id === currentSelected); return node ? [...result, node.graphKey] : result; }, []) console.log(selectedNodeIds);
push
const nodes = [ {id: 1, graphKey: 'key1'}, {id: 9, graphKey: 'key9'}, {id: 3, graphKey: 'key3'}, {id: 4, graphKey: 'key4'}, {id: 5, graphKey: 'key5'}, ] const selected = [1,2,3,4] const selectedNodeIds = selected .map((sel) => { const neededNode = nodes.filter((n) => n.id === sel); return neededNode.length ? neededNode[0].graphKey : undefined; }) .filter((sel) => sel); console.log(selectedNodeIds);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
push
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/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
117992.9 Ops/sec
push
115177.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents two individual test cases for measuring performance differences between `spread` and `push` operations in JavaScript arrays. The benchmark definition is not explicitly defined in the JSON, but based on the script preparation code, it appears that the test is comparing the performance of two approaches: 1. **Spread operation**: The first approach uses the spread operator (`...`) to create a new array by copying elements from another array. 2. **Push operation**: The second approach uses the `push` method to add one or more elements to an existing array. **Script Preparation Code** The script preparation code defines two arrays, `nodes` and `selected`, which are used in both test cases. The `nodes` array contains objects with `id` and `graphKey` properties, while the `selected` array contains IDs that correspond to specific nodes in the `nodes` array. **Test Cases** The two test cases differ only in how they manipulate the `nodes` and `selected` arrays: 1. **Spread operation**: The code uses the spread operator (`...`) to create a new array `selectedNodeIds`, which is then logged to the console. 2. **Push operation**: The code uses the `map` method to transform the `selected` array into an array of node IDs, and then filters out any undefined values using another `filter` method. **Options Compared** The two test cases compare the performance of two different approaches: 1. **Spread operation**: This approach creates a new array by copying elements from another array. 2. **Push operation**: This approach uses the `push` method to add one or more elements to an existing array. **Pros and Cons** Here are some pros and cons of each approach: * **Spread operation**: + Pros: - Efficient for large arrays - Creates a new, independent copy of the original array + Cons: - May incur additional memory allocation overhead - Can be slower than `push` for small arrays * **Push operation**: + Pros: - Faster for small arrays - Does not require additional memory allocation + Cons: - Modifies the original array - May incur performance degradation if the array is large and needs to be reallocated frequently **Library and Special JS Feature** There are no libraries or special JavaScript features explicitly used in these test cases. The code relies on built-in methods like `map`, `filter`, and `reduce`. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Concatenation**: Instead of using the spread operator, you could concatenate arrays using the `concat` method: `const newNodes = [...nodes, ...selected];` * **Array slicing**: You could use array slicing to create a subset of the original array: `const selectedNodeIds = nodes.slice(selected);` * **For loops**: Instead of using the `map` and `filter` methods, you could use for loops to iterate over the arrays and perform the desired operations. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the original spread and push operations.
Related benchmarks:
Push vs Spread stuff
spread operator vs push test - correct
Pushing items via Array.push vs. Spread Operator
spread operator vs push Brian
spread operator vs push Brian2
Comments
Confirm delete:
Do you really want to delete benchmark?