Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get file extension (String v.s. Regex)
(version: 0)
Get file extension from path and name
Comparing performance of:
String.split vs String.split (one-liner, mutation) vs Regex.exec
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// There are 100 more files var filePaths = new Array(250).fill('').map(() => (crypto.randomUUID() + crypto.randomUUID()) .slice(0, 32) .replace(/c/g, '.') .replace(/(0|9|a)/g, '/'), ); var fileExt;
Tests:
String.split
for (const path of filePaths) { const args = path.split('.'); fileExt = args[args.length - 1]; }
String.split (one-liner, mutation)
for (const path of filePaths) { fileExt = path.split('.').pop(); }
Regex.exec
for (const path of filePaths) { fileExt = /\.([^\.]+)$/.exec(path)?.[1]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String.split
String.split (one-liner, mutation)
Regex.exec
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.split
23269.8 Ops/sec
String.split (one-liner, mutation)
23581.4 Ops/sec
Regex.exec
26574.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **What is being tested?** The provided benchmark tests three different ways to extract the file extension from a path and name: 1. `String.split()`: Using the built-in string method `split()` with a dot (`.`) as the separator. 2. `String.split (one-liner, mutation)`: A one-liner version of the previous approach, which is likely optimized for performance by using a single call to `split()` and then extracting the last element of the resulting array. 3. `Regex.exec()`: Using regular expressions to extract the file extension from the path. **Comparison of options** 1. **`String.split()`**: This method uses a simple string splitting approach, which can be efficient but may not always produce the correct result (e.g., if the path contains multiple consecutive dots). * Pros: Easy to implement and understand. * Cons: May not be as performant as other methods, especially for large inputs. 2. **`String.split (one-liner, mutation)`**: This approach uses a single call to `split()` followed by an array manipulation to extract the last element of the resulting array. * Pros: Optimized for performance and concise implementation. * Cons: May be less readable than other approaches, especially for those not familiar with array manipulation. 3. **`Regex.exec()`**: This method uses regular expressions to match the file extension against a pattern. * Pros: Highly flexible and powerful, but may also be slower due to the overhead of regex compilation. **Library used** None of the test cases explicitly use any external libraries beyond JavaScript's built-in functions (`String.split()`, `Array.map()`). **Special JS features or syntax** There are no explicit mentions of special JavaScript features or syntax in the benchmark code. However, the `crypto` module is used to generate random UUIDs for the file paths. **Benchmark preparation code explanation** The script generation code creates an array of 250 randomly generated file paths, each consisting of two parts separated by a dot (`.`). The `replace()` method is used to replace certain characters in the path with dots (`.`) and slashes (`/`) to create a realistic file system structure. This allows for testing different file extension extraction approaches on various input scenarios. **Other alternatives** If you were to test alternative file extension extraction methods, some options might include: * Using `path.basename()` and `path.extname()` from the Node.js `path` module * Implementing a custom function to extract the file extension using arithmetic operations or string manipulation * Using a library like `file-type` (a dependency of Node.js) that provides a simple way to determine the file type and extension Keep in mind that these alternatives may not be directly comparable to the original test cases, as they might introduce additional dependencies or change the input data.
Related benchmarks:
Take last part from URL (Regex vs split)
get last element of path: split vs regex
Splitting and element access versus regex
Regex removing whitespace vs trim 3
Get last path part - Regex vs split
Comments
Confirm delete:
Do you really want to delete benchmark?