Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs individual vs apply
(version: 0)
Comparing performance of:
spread vs individual vs Apply vs Individual noprecreated
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from({length: 9999}, () => Array.from({length: 6}, () => Math.random())) function sum(a1, a2, a3, a4, a5, a6) { return a1 + a2 + a3 + a4 + a5 + a6; }
Tests:
spread
let a = 0 for (let i = 0; i < 9999; ++i) { a += sum(...data[i]); } console.log(a)
individual
let a = 0 for (let i = 0; i < 9999; ++i) { let datai = data[i] a += sum(datai[0], datai[1], datai[2], datai[3], datai[4], datai[5]); } console.log(a)
Apply
let a = 0 for (let i = 0; i < 9999; ++i) { a += sum.apply(null, data[i]); } console.log(a)
Individual noprecreated
let a = 0 for (let i = 0; i < 9999; ++i) { let datai = data[i] a += sum(data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5]); } console.log(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
spread
individual
Apply
Individual noprecreated
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
788.7 Ops/sec
individual
1147.1 Ops/sec
Apply
757.7 Ops/sec
Individual noprecreated
278.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark and explore the different approaches. **Benchmark Overview** The benchmark compares three ways to sum up elements from an array of arrays: 1. Using the spread operator (`...`) to flatten the inner array. 2. Accessing individual elements of the inner array by index (e.g., `data[i][0]`). 3. Using the `apply()` method to pass a single array to the `sum()` function. **Script Preparation Code** The script preparation code creates an array of arrays, where each inner array has 6 random numbers. The `sum()` function is defined as a simple addition of all elements in the input array. **Individual Test Cases** Each test case has a slightly different implementation: * "spread": Uses the spread operator to flatten the inner array and pass it to the `sum()` function. + Pros: Simple, concise, and easy to read. The spread operator is a modern feature that's widely supported in JavaScript engines. + Cons: May have performance overhead due to the creation of a new array. * "individual": Accesses individual elements of the inner array by index and passes them to the `sum()` function using separate arguments. + Pros: Can be more efficient than the spread operator approach, as it avoids creating an intermediate array. Also, some older JavaScript engines may optimize this pattern better. + Cons: Requires explicit indexing, which can make the code less readable for complex sums. * "Apply": Uses the `apply()` method to pass a single array to the `sum()` function. The inner array is passed as the first argument, and the individual elements are provided as subsequent arguments. + Pros: Can be more efficient than the spread operator approach, especially when dealing with large arrays. Also, some older JavaScript engines may optimize this pattern better. + Cons: Requires the use of `apply()`, which can make the code less readable for complex sums. Additionally, some modern browsers may have issues with the number of arguments passed to `apply()`. **Individual noprecreated** This test case is similar to "individual", but without creating a new local variable `datai` to hold the inner array. Instead, it directly accesses the inner array elements by index. + Pros: Similar efficiency benefits as "individual". + Cons: Requires explicit indexing, which can make the code less readable for complex sums. **Library Usage** None of these test cases use any external libraries. **Special JS Features or Syntax** None of the benchmark test cases rely on special JavaScript features or syntax beyond what's required to implement the `sum()` function and array operations.
Related benchmarks:
Push to array, vs ES6 Spread.
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array slice() vs slice(0) vs spread operator
spread vs ArrayFrom
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?