Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array immutability vs mutability
(version: 0)
Comparing performance of:
push vs spread
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
push
const data = []; for (let i = 0; i < 10000; ++i) data.push(i);
spread
let data = []; for (let i = 0; i < 10000; ++i) [...data,i];
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:
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 dive into explaining the provided benchmark. **What is tested:** The benchmark tests two approaches to creating an array in JavaScript: 1. `push`: This approach creates an empty array and then uses the `push` method to add elements to it. The array grows in size by 10,000 elements. 2. `spread`: This approach creates an empty array and then uses the spread operator (`[...]`) to create a new array with the original array as its first element, followed by the numbers from 0 to 9,999. **Options compared:** The benchmark compares the performance of these two approaches: * Push vs Spread **Pros and Cons:** * `push`: + Pros: Simple and intuitive. + Cons: Can be slower due to the overhead of resizing the array. * `spread`: + Pros: Faster, as it creates a new array with the desired size. + Cons: Requires understanding of the spread operator and may be less intuitive. **Library usage:** None of the test cases use any libraries. The benchmark only relies on built-in JavaScript features. **Special JS feature or syntax:** The `spread` operator (`[...]`) is used in the second test case. This operator creates a new array by copying elements from an existing array and adds new elements to it. It's a convenient way to create arrays with a desired size, but its performance can vary depending on the browser and JavaScript engine. **Other alternatives:** If you wanted to test different approaches to creating an array in JavaScript, you could consider adding additional test cases for: * `Array.from()`: Creates a new array from an iterable object. * `Array.prototype.slice()`: Creates a shallow copy of a portion of an array. * `Array.prototype.concat()`: Concatenates two or more arrays. Here's an example of how the benchmark definition JSON could be updated to include additional test cases: ```json { "Name": "Array creation", "Description": null, "Script Preparation Code": null, "Html Preparation Code": null } Individual test cases: [ { "Benchmark Definition": "const data = [];\r\nfor (let i = 0; i < 10000; ++i) data.push(i);", "Test Name": "push" }, { "Benchmark Definition": "let data = [];\r\nArray.from(data, () => null);\r\nfor (let i = 0; i < 10000; ++i) data.push(i);", "Test Name": "from" }, { "Benchmark Definition": "let data = [];\r\ndata = [...data];\r\nfor (let i = 0; i < 10000; ++i) data.push(i);", "Test Name": "slice" }, { "Benchmark Definition": "let data = [];\r\ndata = [...data, ...Array.from({ length: 10000 }, () => null)];", "Test Name": "concat" } ] ``` Keep in mind that adding more test cases may increase the complexity of the benchmark and require more time to run.
Related benchmarks:
object spread vs immutable-js set vs object mutate
Immutable Map vs Ordered Map vs List
Spread operator vs Immutable.js performance for common use cases
Immutable.Set Union vs Constructing a new plain JS Set
Immutable.Set Union vs Constructing a new plain JS Set - small
Comments
Confirm delete:
Do you really want to delete benchmark?