Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs split+pop vs lastIndexOf+substring
(version: 0)
Comparing performance of:
RegEx vs split+pop vs lastIndexOf+substring
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'arn:aws:evidently:us-east-1:111111:project/project/feature/newExperience';
Tests:
RegEx
str.match(/[^/]+$/)[0];
split+pop
str.split('/').pop();
lastIndexOf+substring
var n = str.lastIndexOf('/');str.substring(n + 1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx
split+pop
lastIndexOf+substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx
390028.5 Ops/sec
split+pop
19511430.0 Ops/sec
lastIndexOf+substring
28821536.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark measures the performance of three different approaches to extract the last path component from a URL string: 1. `str.match(/[^/]+$/)[0]` (Regular Expression) 2. `str.split('/').pop()` (Split and Pop) 3. `var n = str.lastIndexOf('/') ; str.substring(n + 1)` (Last Index of and Substring) **Options Compared** The three options are compared in terms of their execution speed, which is measured by the number of executions per second. **Pros and Cons of Each Approach** 1. **Regular Expression (`str.match(/[^/]+$/)[0]`)**: * Pros: Can be more efficient for certain use cases where the URL format is complex or varies frequently. * Cons: May have higher overhead due to the complexity of regular expression syntax, which can lead to slower execution times. 2. **Split and Pop (`str.split('/').pop()`)**: * Pros: Simple, intuitive, and easy to understand, making it a popular choice for developers. It's also relatively fast since `split()` is optimized in JavaScript. * Cons: May not be suitable for complex URL formats or frequent string manipulation, as it can lead to slower execution times due to the overhead of splitting strings. 3. **Last Index of and Substring (`var n = str.lastIndexOf('/') ; str.substring(n + 1)`)**: * Pros: Can be efficient for simple URL formats with a fixed number of path components. It's also relatively fast since it avoids the overhead of regular expressions or string splitting. * Cons: May not be suitable for complex URL formats, as it requires manual indexing and substring extraction, which can lead to slower execution times. **Library Usage** The test case uses the `lastIndexOf()` method from the JavaScript built-in functions, which is part of the ECMAScript standard. This method is used to find the last occurrence of a specified value (in this case, the `/` character) in a string and returns its index. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark that require additional explanation beyond what's already discussed. **Other Alternatives** In addition to these three approaches, other alternatives could be: * Using `URL.prototype.pathname` (a newer API introduced in ECMAScript 2018) to extract the last path component. * Implementing a custom solution using loop-based string manipulation or regular expressions with a more optimized implementation. * Comparing different libraries like `js-string-extras` or `string-polyfill`, which provide utility functions for working with strings. Keep in mind that these alternatives may not be part of the standard JavaScript syntax and might require additional setup or dependencies.
Related benchmarks:
regex vs split/pop
GetName
regex vs split+pop
Regex vs Split and pop node name
Comments
Confirm delete:
Do you really want to delete benchmark?