Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string split vs spread vs array from
(version: 0)
Comparing performance of:
Array from vs Spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "foo.bar"; var s2 = "foo";
Tests:
Array from
var n1 = Array.from(s1) var n2 = Array.from(s2)
Spread
var n1 = [...s1]; var n2 = [...s2];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array from
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
29 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array from
9380466.0 Ops/sec
Spread
13018645.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this specific benchmark. **What is being tested?** The provided JSON represents two test cases: `Array.from` and Spread syntax (using the spread operator). The goal is to measure the performance difference between these two approaches when splitting a string. **Options compared:** There are two options being compared: 1. **Array.from**: This method creates a new array from an iterable or an array-like object. 2. **Spread**: This syntax uses the spread operator (`...`) to create a new array by spreading the elements of an array-like object. **Pros and Cons:** * **Array.from**: + Pros: More explicit, readable, and maintainable way to create arrays from iterables or array-like objects. + Cons: May incur additional overhead due to the creation of an intermediate array. * **Spread**: + Pros: Concise, efficient, and can be faster than `Array.from`. + Cons: Less readable and maintainable for complex cases, and may require careful consideration of edge cases. **Other considerations:** When choosing between these two approaches, consider the following: * Readability and maintainability are essential for large-scale codebases. If your code is already using `Array.from`, it might be more suitable to stick with that approach. * Performance-critical sections of code may benefit from using Spread syntax, as it can generate native machine code for array creation. **Library usage:** There doesn't seem to be any external libraries used in this benchmark. The only library-like feature being tested is the `Array.from` method itself. **Special JS features or syntax:** This benchmark uses a special JavaScript feature: the spread operator (`...`). Introduced in ECMAScript 2015, it allows you to create new arrays by spreading elements of an array-like object. **Benchmark preparation code:** The script preparation code is very simple: ```javascript var s1 = "foo.bar"; var s2 = "foo"; ``` These variables are created as string literals. **Individual test cases:** Each test case consists of a single benchmark definition, which defines the input strings `s1` and `s2`. The test names indicate that we're testing both `Array.from` and Spread syntax: * **Test Case 1:** `Array from` ```javascript var n1 = Array.from(s1); var n2 = Array.from(s2); ``` * **Test Case 2:** Spread ```javascript var n1 = [...s1]; var n2 = [...s2]; ``` **Latest benchmark result:** The results show that the Spread syntax (`[...s1]` and `[...s2]`) outperformed the `Array.from` method in this specific benchmark. Other alternatives for creating arrays from iterables or array-like objects include: * Using the `map()` function with an arrow function * Using a custom implementation using a `for` loop
Related benchmarks:
Array.prototype.slice vs spread operator copy
Array.prototype.slice vs spread operator1
Which is faster Array.prototype.slice vs spread operator
Javascript array cloning slice vs spread
Array.prototype.slice vs spread operator new
Comments
Confirm delete:
Do you really want to delete benchmark?