Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array split vs string substring after
(version: 0)
Comparing performance of:
Array.split vs Substring
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var s1 = "foo.bar"; var s2 = "foo";
Tests:
Array.split
var n1 = s1.split(".").pop(); var n2 = s2.split(".").pop();
Substring
var n1 = s1.substring(s1.indexOf(".")+1); var n2 = s2.substring(s2.indexOf(".")+1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.split
Substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.split
5031603.5 Ops/sec
Substring
4372673.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to extract the last part of a string: using `Array.split()` with `pop()`, and using the `substring` method. **Approaches Compared** 1. **Array Split with Pop**: This approach uses the `split()` method to split the input string into an array, and then uses the `pop()` method to get the last element of the array. 2. **Substring**: This approach uses the `indexOf()` method to find the index of the last dot (`.`) in the input string, and then uses the `substring` method to extract the substring starting from the next character. **Pros and Cons** * **Array Split with Pop**: + Pros: Simple and efficient way to get the last part of a string. + Cons: May not be as fast as other approaches for very large strings, as it creates an array and then pops the last element. * **Substring**: + Pros: More flexible than `Array.split()` with `pop`, as it can extract any substring starting from a specific index. + Cons: May be slower than `Array.split()` with `pop` for small strings, since it involves finding the index of the dot (`.`) using `indexOf()`. * **Other Considerations**: + Both approaches assume that the input string has at least one dot (`.`). If this is not guaranteed, additional checks may be necessary. **Library/Functionality Used** None. This benchmark only uses built-in JavaScript functions and methods. **Special JS Feature/Syntax** No special features or syntax are being tested in this benchmark. **Alternative Approaches** Other approaches to extract the last part of a string include: * Using `lastIndexOf()` instead of `indexOf()` * Using a regular expression with the `exec` method * Using a library like jQuery's `.split()` and `.slice()` methods These alternative approaches may have their own pros and cons, depending on the specific use case.
Related benchmarks:
Array split vs string substring-1
Array split vs string substring2
Array split vs string substring for dates
Array split vs string substring ISO String
Array split vs string substring22
Comments
Confirm delete:
Do you really want to delete benchmark?