Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
get last element of path: split vs regex
(version: 0)
Comparing performance of:
get last element of path: split vs get last element of path: regex
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var text = '/test/abcd/'
Tests:
get last element of path: split
text.split('/').filter(s => s)
get last element of path: regex
text.match(/([^/]+)\/?$/)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
get last element of path: split
get last element of path: regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
Browser/OS:
Safari 15 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
get last element of path: split
1665321.8 Ops/sec
get last element of path: regex
2241349.8 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two methods for extracting the last element from a file path string: **Method 1: `split()` and `filter()`:** * This method first uses `text.split('/')` to split the path string into an array of segments separated by slashes. * Then, it uses `filter(s => s)` to keep only non-empty segments in the array. This effectively removes any empty strings that might result from consecutive slashes. **Method 2: Regular Expression (`match()`):** * This method uses a regular expression `text.match(/([^/]+)\\/?$/)` to find the last element of the path. * `([^/]+)` captures one or more characters that are not slashes, representing the last file name or directory. * `\\/?$` matches an optional trailing slash followed by the end of the string (`$`). **Pros and Cons:** | Method | Pros | Cons | |---------------|---------------------------------------|----------------------------------------------| | `split() + filter()` | - Easy to understand | - Can be less performant for large paths | | `regex` | - Often more performant for complex paths | - More complex syntax, potentially harder to read and debug | **Other Considerations:** * **Path Complexity:** For simple paths, the difference in performance between the two methods might be negligible. However, as path complexity increases (more segments, nested directories), the `regex` method may become significantly faster due to its ability to directly target the desired element. * **Readability and Maintainability:** The `split()` + `filter()` approach is generally considered more readable for simple use cases. While regular expressions can be powerful, they often introduce complexity that can make code harder to understand and maintain, especially for developers unfamiliar with regex syntax. **Alternatives:** * **Node.js `path` module:** This module provides built-in functions like `basename()` which are specifically designed for working with file paths and offer a more streamlined and reliable approach compared to manual string manipulation. Let me know if you have any other questions!
Related benchmarks:
Take last part from URL (Regex vs split)
Splitting and element access versus regex
split vs regex onurl
Get last path part - Regex vs split
Comments
Confirm delete:
Do you really want to delete benchmark?