Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array split vs string substring vs substr
(version: 0)
Comparing performance of:
Array.split vs Substring vs substr
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "foo.bar"; var s2 = "foo";
Tests:
Array.split
var n1 = s1.split(".")[0]; var n2 = s2.split(".")[0];
Substring
var n1 = s1.substring(0, s1.indexOf(".")); var n2 = s2.substring(0, s2.indexOf("."));
substr
var n1 = s1.substr(0, s1.indexOf(".")); var n2 = s2.substr(0, s2.indexOf("."));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.split
Substring
substr
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):
The provided JSON represents a benchmark test that compares the performance of three different approaches to split or substring strings in JavaScript: 1. `split()`: The `split()` method splits a string into an array of substrings, using a specified separator. In this case, the separator is `"."`. 2. `substring()`: The `substring()` method returns a new string that includes a portion of another string, starting from a specified start index and ending at a specified end index. 3. `substr()`: The `substr()` method returns a substring of an existing string, starting from a specified start index and having a specified length. The test cases are designed to measure the performance of each approach on two strings: `s1 = "foo.bar"` and `s2 = "foo"`. Each test case splits or substrings one of these strings using one of the three approaches, and measures the time taken to complete the operation. **Pros and Cons of each approach:** * `split()`: This approach is generally faster than the other two, especially for larger inputs. However, it creates a new array of substrings, which can be memory-intensive. Additionally, if the separator is not present in the input string, this method will return an empty array. * `substring()`: This approach is slower than `split()` but faster than `substr()`. It requires specifying the start and end indices, which can add overhead. However, it does not create new arrays or strings, making it a good choice for performance-critical code. * `substr()`: This approach is slowest among the three, especially for larger inputs. It requires specifying both the start index and length of the substring to be extracted. **Other considerations:** * The test cases use JavaScript's built-in string methods, which means that the results will reflect the performance characteristics of the JavaScript engine being used. * The `DevicePlatform` and `OperatingSystem` fields in the benchmark result indicate that the test was run on a desktop device with Mac OS X 10.15.7. **Library usage:** None of the provided code snippet uses any external libraries or dependencies. **Special JS features or syntax:** There are no special JavaScript features or syntax used in this benchmark. **Alternative approaches:** If you need to split or substring strings, you may also consider using regular expressions or other string manipulation techniques. For example: * Using a regular expression to match the separator and extract the substrings. * Using a library like Lodash or Underscore.js for more advanced string manipulation techniques. However, it's worth noting that the `split()`, `substring()`, and `substr()` methods are often the most efficient and convenient ways to perform basic string splitting and substring extraction in JavaScript.
Related benchmarks:
Array from vs string split
Array split vs string substring for dates
Array split vs string substring ISO String
Array split vs string substring22
String.split vs String.substring
Comments
Confirm delete:
Do you really want to delete benchmark?