Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp vs String vs Arrays manipulation for paths
(version: 0)
Comparing performance of:
RegExp Implementation vs String Implementation vs Array Implementation
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const routes = [{ path: '/user/1234', content: 'User 1234' }, { path: '/event/5678/pictures/starred', content: 'Event Pictures' }, { path: '/events', content: 'Events' }, ]; function routeResolverRegex(path) { function normalizePath(p) { return p.replace(/\/$/, '') } path = normalizePath(path) const route = routes.find(r => { const re = new RegExp('^' + normalizePath(r.path) + '$') return re.test(path) }) return route?.content ?? '<div>Not Found</div>' } function routeResolverString(path) { path = path.endsWith('/') ? path.slice(0, -1) : path; const route = routes.find(r => r.path === path); return route?.content ?? '<div>Not Found</div>' } function routeResolverArray(path) { const segments = path.split('/').filter(Boolean); const route = routes.find(r => segments.join('/') === r.path); return route?.content ?? '<div>Not Found</div>' }
Tests:
RegExp Implementation
routeResolverRegex('/user/1234');
String Implementation
routeResolverString('/user/1234');
Array Implementation
routeResolverArray('/user/1234');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegExp Implementation
String Implementation
Array Implementation
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegExp Implementation
1817814.1 Ops/sec
String Implementation
17009436.0 Ops/sec
Array Implementation
1722672.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark and its various components. **Benchmark Definition** The provided JSON represents a benchmark test case for comparing the performance of three different approaches to resolve routes in a route resolver function: regular expressions (RegExp), strings manipulation, and arrays manipulation. **Options Compared** 1. **Regular Expressions (RegExp)**: The RegExp implementation uses a regular expression to match the input path against each route path. It's likely using the `RegExp.test()` method. 2. **Strings Manipulation**: The string implementation uses a simple string manipulation function that removes trailing slashes and compares the result with each route path. 3. **Arrays Manipulation**: The array implementation splits the input path into segments, filters out empty strings, and joins them back together to form a new string, then compares it with each route path. **Pros and Cons of Each Approach** 1. **RegExp Implementation** * Pros: + Can handle more complex route paths. + Can be more efficient for large datasets. * Cons: + May be slower due to the overhead of regular expression creation and execution. + Requires a larger amount of memory for the compiled regular expression. 2. **Strings Manipulation** * Pros: + Fast and lightweight. + Easy to implement. * Cons: + Limited to simple route paths. + May be slower for large datasets due to string manipulation overhead. 3. **Arrays Manipulation** * Pros: + Fast and lightweight. + Easy to implement. * Cons: + Requires additional memory for the segment array. + May be slower than RegExp implementation due to array creation and joining overhead. **Library Used** In this benchmark, no specific libraries are used beyond the built-in JavaScript `RegExp` class. However, if we were to extend this benchmark to use external libraries, some possible options could include: * For arrays manipulation: `lodash` or `underscore` for efficient string manipulation. * For performance optimization: `fast-regex` or `regex-perf` for optimized regular expression creation and execution. **Special JS Features** There are no special JavaScript features used in this benchmark beyond the standard features of the languages mentioned above (e.g., arrow functions, template literals). **Other Alternatives** For alternative implementations or optimizations, some possible approaches could include: * Using `path.normalize()` to normalize the route paths and simplify comparisons. * Caching the compiled regular expression for repeated use. * Using a more efficient string manipulation algorithm, such as `substring()` instead of `slice()`. * Parallelizing or multi-threading the benchmark execution using Web Workers or Node.js clusters. These are just some possible ideas, and the actual implementation details would depend on the specific requirements and performance characteristics of the route resolver function.
Related benchmarks:
get last element of path: split vs regex
Test Lodash Get speed
endsWith slice vs Regex replace
regex vs startsWith vs indexOf perf on url
Comments
Confirm delete:
Do you really want to delete benchmark?