Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js array copy any type
(version: 0)
Comparing performance of:
map vs array.from vs for loop vs forIn vs slice vs spred
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({ length: 65535 }, (_, index) => index); var identity = value => value; var index = 0; while (++index < 10002) { arr = arr.map(identity); arr = Array.from(arr); arr = arr.slice() }
Tests:
map
((identity, newArr) => newArr = arr.map(e=>e))(identity, [])
array.from
var newArr = Array.from(arr)
for loop
var newArr = []; var i = 0; var length = 65535; for(; i < length; i++) { newArr[i] = arr[i]; }
forIn
var newArr = []; for(var i in arr) { newArr[i] = arr[i]; }
slice
var newArr = arr.slice()
spred
var newArr = [...arr];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
map
array.from
for loop
forIn
slice
spred
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 measures the performance of different methods to copy an array in JavaScript, including `Array.from()`, `slice()`, `spred` (using the spread operator), `for loop`, and `map`. The test case starts with a large array of 65535 elements created using `Array.from()`. **Options Compared** Here's what each option is being tested: 1. **Array.from()**: This method creates a new array by mapping over an existing array. 2. **slice()**: This method returns a shallow copy of a portion of an array. 3. **spred (Spread Operator)**: The spread operator (`...`) can be used to create a new array by spreading the elements of an existing array. 4. **for loop**: A traditional `for` loop is used to iterate over each element in the original array and assign it to a new array. 5. **map()**: This method applies a given function to every element of an array. **Pros and Cons** Here's a brief summary of the pros and cons of each option: 1. **Array.from()**: * Pros: Modern, efficient, and concise. * Cons: May incur additional overhead due to the need for an intermediate array. 2. **slice()**: * Pros: Fast and efficient, with minimal overhead. * Cons: Returns a shallow copy, which may not be suitable for all use cases. 3. **spred (Spread Operator)**: * Pros: Modern, concise, and flexible. * Cons: May incur additional overhead due to the need for an intermediate array. 4. **for loop**: * Pros: Classic, efficient, and easy to understand. * Cons: Can be verbose and error-prone. 5. **map()**: * Pros: Modern, concise, and flexible. * Cons: May incur additional overhead due to the need for an intermediate array. **Library Usage** None of the options rely on external libraries. **Special JS Features or Syntax** The benchmark does not use any special JavaScript features or syntax beyond what's available in modern browsers (ECMAScript 2022). **Other Alternatives** If you're interested in exploring other methods to copy arrays, here are a few alternatives: 1. **concat()**: This method concatenates two or more arrays and returns a new array. 2. **reduce()**: This method applies a given function to each element of an array and reduces it to a single value. 3. **filter()**: This method creates a new array with all elements that pass the test implemented by the provided function. These alternatives may have different performance characteristics, pros, and cons compared to the options being tested in this benchmark.
Related benchmarks:
Array clone
Shallow Copy Array
shallow copy of 6M elements array
Array cloning 2023
Comments
Confirm delete:
Do you really want to delete benchmark?