Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ci - spread vs push
(version: 0)
Comparing performance of:
spread vs push
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * max); } function createItem(type, i) { return (type === "buzz") ? { type: "buzz", m: Math.random(), s: Math.random() } : { type: type, subvals: { a: Math.random(), b: Math.random(), c: Math.random(), d: Math.random(), } } } var baseItems = []; for (let i = 0; i < 100; i++) { const type = i % 3 === 0 ? "fizz" : i % 5 === 0 ? "buzz" : "whatever"; baseItems.push(createItem(type, i)); }
Tests:
spread
var spreadItems = baseItems.reduce((acc, current) => { if(current.type === "buzz") { acc.buzz = [...acc.buzz, { a: current.m }] } else { acc.fizzyWhatevs = [...acc.fizzyWhatevs,{ a: current.subvals.a }] } return acc; }, { fizzyWhatevs: [], buzz: [] })
push
var pushItems = baseItems.reduce((acc, current) => { if(current.type === "buzz") { acc.buzz.push({ a: current.m }) } else { acc.fizzyWhatevs.push({ a: current.subvals.a }) } return acc; }, { fizzyWhatevs: [], buzz: [] })
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
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 Overview** The benchmark is comparing two approaches to manipulate an array of objects: `Array.prototype.push()` versus using `Array.prototype.reduce()` with object spreading (`...`). **Test Cases** There are two test cases: 1. `push`: This test case uses the traditional `push()` method to add elements to the `fizzyWhatevs` and `buzz` arrays. 2. `spread`: This test case uses the spread operator (`...`) to create a new array and add elements to it, which is then used as the value of the `fizzyWhatevs` or `buzz` property in the accumulator object. **Library** In both test cases, the `Array.prototype.reduce()` method is used, but no specific library is required. However, if we consider the spread operator (`...`) introduced in ECMAScript 2018 (ES2020), it's worth noting that older browsers may not support it. **Special JS Feature/Syntax** The benchmark uses a feature that was introduced in ECMAScript 2019 (ES2020): the optional chaining operator (`?.`) is not used, but the spread operator (`...`) is. However, there's no special syntax or feature being tested in this benchmark. **Options Compared** Two options are compared: 1. **`Array.prototype.push()`**: This traditional approach uses the `push()` method to add elements to an array. 2. **Spread Operator (`...`)**: This modern approach uses object spreading to create a new array and add elements to it. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: **`Array.prototype.push()`** Pros: * Widely supported across all browsers * Fast and efficient * Well-established API Cons: * Can lead to performance issues when working with large datasets due to frequent array resizing **Spread Operator (`...`)** Pros: * Modern and efficient for creating new arrays * Reduces memory allocation and copying of elements * Better suited for large datasets Cons: * Requires ECMAScript 2018 (ES2020) support in older browsers * Can be slower than traditional `push()` due to the overhead of object creation **Other Considerations** When choosing between these two approaches, consider the following factors: * **Browser Support**: If you need to support older browsers, use the traditional `push()` method. * **Performance**: For large datasets, the spread operator (`...`) is generally faster and more efficient. * **Readability**: Use the spread operator (`...`)) when creating new arrays to improve code readability. **Alternatives** Other alternatives for array manipulation include: 1. `Array.prototype.map()`: Transforms an array by applying a function to each element, returning a new array. 2. `Array.prototype.filter()`: Creates a new array with all elements that pass the test implemented by a provided function. 3. `Array.prototype.slice()`: Returns a shallow copy of a portion of an array. In this benchmark, neither of these alternatives is used.
Related benchmarks:
JavaScript spread operator vs Slice/Splice performance - 2
JavaScript spread operator vs Slice/Splice performance, passing
JavaScript spread operator vs Slice/Splice performance 2
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?