Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.from vs array slice
(version: 0)
Comparing performance of:
Array.from vs Array slice
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var testIterable = function* generateIterable() { for (let i = 0; i < 100; i++) { yield i; } }
Tests:
Array.from
const arr = Array.from(testIterable());
Array slice
const arrs = Array.prototype.slice.call(testIterable());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from
Array slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
573457.3 Ops/sec
Array slice
10643649.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance of two approaches to create an array from an iterable: 1. `Array.from()` 2. `Array.prototype.slice.call()` (often referred to as "array slice") In this specific case, the test case uses a generator function `testIterable` to generate an array of numbers from 0 to 99. **Options compared** The benchmark compares two options for creating an array from an iterable: 1. **`Array.from()`**: This method creates a new array from an iterable (such as an array, object, or any other type of iterable) by mapping each element to a new array value. 2. **`Array.prototype.slice.call()`** (or simply "array slice"): This approach uses the `slice()` method on an array and then passes the result to `call()` with an array of arguments. **Pros and Cons** Here are some pros and cons of each approach: 1. **`Array.from()`**: * Pros: + More concise and expressive syntax. + Often faster, as it can take advantage of optimized internal algorithms. * Cons: + May have performance issues if the iterable is very large or complex. 2. **`Array.prototype.slice.call()` (array slice)**: * Pros: + Can be more efficient for very large iterables, as it allows the browser to optimize the `slice()` method. * Cons: + Less concise and expressive syntax compared to `Array.from()`. + May have additional overhead due to the use of `call()`. **Library usage** In this benchmark, no external libraries are used. However, if you were to extend this benchmark to compare other approaches, you might consider using libraries like Lodash or Ramda, which provide a range of utility functions for working with arrays and iterables. **Special JavaScript feature/syntax** There is no special JavaScript feature or syntax mentioned in this benchmark. The test cases rely solely on standard JavaScript features and syntax. **Other alternatives** If you wanted to compare other approaches to creating an array from an iterable, some possible alternatives could include: * Using `Array.prototype.concat()` to concatenate multiple iterables. * Using a for loop with indexing to create the array. * Using a custom implementation of an iterator or generator function. Keep in mind that each of these alternative approaches would have its own pros and cons, which would need to be carefully considered when designing a benchmark.
Related benchmarks:
Slice & Splice vs ES6 Array Spread
non-mutating array remove: spread and slice vs slice and splice
slice vs get by index
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?