Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
split() vs Array.from() vs push()
(version: 0)
Comparing performance of:
split() vs Array.from() vs push() -> for of vs push -> for
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
split()
let arr = "abcdef".split("");
Array.from()
let arr = Array.from("abcdef");
push() -> for of
let arr = []; for(let e of "abcdef") arr.push(e);
push -> for
let s = "abcdef"; let arr = []; for(let i = 0; i<s.length - 1; i++) arr.push(s[i]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
split()
Array.from()
push() -> for of
push -> for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
split()
106448592.0 Ops/sec
Array.from()
13651917.0 Ops/sec
push() -> for of
40797200.0 Ops/sec
push -> for
55866668.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript microbenchmark on the website MeasureThat.net, specifically comparing three approaches to split a string into an array: `split()`, `Array.from()`, and `push()` with two different iteration methods (`for...of` and `for`). The benchmark is designed to measure the performance of these methods across various browsers and devices. **Options Compared** The three options being compared are: 1. **`split()`**: A method that splits a string into an array using a specified separator. 2. **`Array.from()`**: A method that creates a new array from an iterable (such as a string) using the `from()` method. 3. **`push()` with for...of iteration** and **`push()` with traditional for loop**: Two approaches to iterate over a string and push its characters into an array. **Pros and Cons of Each Approach** ### `split()` * Pros: + Widely supported by most browsers + Can handle multiple separators + Returns an array of substrings split at the specified separator * Cons: + May be slower than other methods for large strings due to regular expression parsing + Can lead to performance issues if the separator is a special character ### `Array.from()` * Pros: + Fast and efficient, especially for large strings + Can handle multiple iterables (including strings) + Returns an array of values from the iterable * Cons: + Requires modern browsers that support the `from()` method + May not be as intuitive or readable as other methods ### `push()` with for...of iteration and traditional for loop * Pros: + Easy to implement and read, especially for developers familiar with `for` loops + Can be faster than `split()` and `Array.from()` due to less overhead * Cons: + May not be as concise or elegant as other methods + Requires manual management of the loop and array **Libraries Used** None are explicitly mentioned in the provided JSON. However, it's worth noting that some browsers may use internal libraries or modules under the hood for string manipulation. **Special JS Features/ Syntax** * `for...of` iteration is used in two test cases to iterate over a string. * The `from()` method is used in one test case with `Array.from()`. * Regular expressions are not explicitly mentioned, but they may be involved in the parsing of separators and other special characters. **Other Alternatives** Some alternative approaches for splitting strings into arrays include: 1. **`reduce()`**: A method that applies a reduction function to each element of an array. 2. **`map()`**: A method that creates a new array from an iterable by applying a transformation function to each element. 3. **`slice()`** or `substring()`: Methods that return a substring of the original string, which can be used to create an array. However, these alternatives may have different performance characteristics and use cases compared to the methods being tested in this benchmark.
Related benchmarks:
Slice & Splice vs ES6 Array Spread
Array.prototype.slice vs spread operator with length limit
Array.prototype.slice vs Array.from()
Which is faster Array.prototype.slice vs spread operator
Array.prototype.slice vs spread op
Comments
Confirm delete:
Do you really want to delete benchmark?