Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread operator vs. Array.map change elemet
(version: 0)
Comparing performance of:
Spread vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src=''></script>
Script Preparation code:
permutations = new Array(10000).map(() => ({ pagination: 'blah blah blah' })); var index = 500;
Tests:
Spread
let newArr = [...permutations] newArr[index] = 'NEW blah blah blah'; return newArr;
Map
const newArr = permutations.map((item, i) => { if (i === index) { return item = 'NEW blah blah blah'; } else { return item; } }) return newArr;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
12213.4 Ops/sec
Map
131006.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The provided JSON represents a JavaScript microbenchmark that measures the performance of two approaches for modifying an array: 1. Using the spread operator (`...`). 2. Using the `Array.map()` method with a callback function. **Options Compared:** * **Spread Operator:** Creates a new array by spreading the elements of the original array into a new array. + Pros: - More concise and readable code. - Faster execution time, since it avoids creating an intermediate array. + Cons: - Can lead to performance issues if dealing with large arrays, as it involves creating a new array and copying elements. * **Array.map():** Creates a new array by applying a callback function to each element of the original array. + Pros: - More flexible, as it allows for more complex transformations. - Can be used in conjunction with other methods (e.g., `forEach()`). + Cons: - Slower execution time due to the overhead of creating a new array and applying the callback function. **Library:** There is no explicit library mentioned in the provided JSON. However, it's worth noting that the `Array.map()` method is a built-in JavaScript method. **Special JS Feature/Syntax:** The benchmark uses the spread operator (`...`), which was introduced in ECMAScript 2015 (ES6). It allows for concise array creation and has become a popular feature in modern JavaScript development. **Benchmark Preparation Code:** The script preparation code creates an array of 10,000 objects with a `pagination` property. The `index` variable is set to 500. This data structure serves as the input for both benchmark cases. **Individual Test Cases:** 1. **Spread Operator:** ```javascript let newArr = [...permutations] newArr[index] = 'NEW blah blah blah' return newArr ``` This code uses the spread operator to create a new array, `newArr`, which is then modified by assigning a new value at index 500. 2. **Array.map():** ```javascript const newArr = permutations.map((item, i) => { if (i === index) { return item = 'NEW blah blah blah' } else { return item } }) return newArr ``` This code uses the `Array.map()` method to apply a callback function to each element of the original array. If the index matches, it assigns a new value; otherwise, it returns the original element. **Benchmark Results:** The latest benchmark results show that Firefox 124 performs better with the spread operator (`ExecutionsPerSecond` = 131006.90625) compared to `Array.map()` (`ExecutionsPerSecond` = 12213.3818359375). **Other Alternatives:** * **Using a loop:** Instead of using the spread operator or `Array.map()`, you could use a traditional `for` loop to iterate over the array and modify elements individually. ```javascript let newArr = new Array(10000).fill(null) for (let i = 0; i < 10000; i++) { if (i === index) { newArr[i] = 'NEW blah blah blah' } } return newArr ``` * **Using a custom library:** Depending on the specific requirements, you might consider using a specialized library like Lodash or Ramda to perform array operations. However, this would likely add overhead and may not be necessary for simple use cases. Keep in mind that performance differences between these approaches will vary depending on factors such as array size, complexity of modifications, and specific hardware configurations.
Related benchmarks:
Array.from vs Spread with undefined and map
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array from vs Spread es6
unshift vs spread
spread vs ArrayFrom
Comments
Confirm delete:
Do you really want to delete benchmark?