Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
path regex
(version: 0)
Comparing performance of:
precomp vs literal
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var precomp = new RegExp(/[^A-Za-z0-9.-]+|(?=[-A-Za-z])|(?<=[A-Za-z])|(?<=\.\d+)(?=\.)/g); var test = "M 0 0 L 10 10 L10 10 L20 20 M 0 0 L 10 10 L10 10 L20 20 M 0 0 L 10 10 L10 10 L20 20 M 0 0 L 10 10 L10 10 L20 20";
Tests:
precomp
test.split(precomp);
literal
test.split(/[^A-Za-z0-9.-]+|(?=[-A-Za-z])|(?<=[A-Za-z])|(?<=\.\d+)(?=\.)/g);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
precomp
literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
precomp
20032.2 Ops/sec
literal
20092.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested. **Benchmark Overview** MeasureThat.net provides a platform for testing JavaScript microbenchmarks, where users can create and run their own benchmarks or use pre-existing ones. The current benchmark is defined in two parts: 1. **Script Preparation Code**: This section defines a regular expression (`precomp`) that will be used to test the performance of various approaches. 2. **Individual Test Cases**: These are two separate tests that demonstrate different approaches for splitting a string using the `split()` method. **Test Cases** Let's analyze each test case: ### Test Case 1: "precomp" This test uses the `new RegExp()` constructor to create a regular expression (`precomp`) and then passes it to the `split()` method. ```javascript var precomp = new RegExp(/[^A-Za-z0-9.-]+|(?=[-A-Za-z])|(?<=[A-Za-z])|(?<=\\.\\d+)(?=\\.)/g); ``` The regular expression `precomp` matches one or more of the following patterns: * Non-alphanumeric characters (`[^A-Za-z0-9.-]`) * A hyphen followed by an alphabetic character (`[-A-Za-z]`) * An alphabetic character preceded by another alphabetic character (`(?<=[A-Za-z])`) * A dot followed by a digit (`(?<=\\.\\d+)(?=\\.)`) **Pros and Cons** Using `new RegExp()` can be beneficial when you need to create a complex regular expression, but it comes with some drawbacks: * Performance overhead due to the creation of a new regex object * Potential security risks if not validated properly **Test Case 2: "literal"** This test uses a literal regular expression (`/[^A-Za-z0-9.-]+|[-A-Za-z]|[\w]|\.\d+\.`) directly in the `split()` method. ```javascript test.split(/[^A-Za-z0-9.-]+|[-A-Za-z]|[\w]|\.\d+\.\/g); ``` The regular expression matches one or more of the following patterns: * Non-alphanumeric characters (`[^A-Za-z0-9.-]`) * A hyphen followed by an alphabetic character (`[-A-Za-z]`) * Word characters (`\w`) * A dot followed by a digit (`\.\d+\.`) **Pros and Cons** Using a literal regular expression can be beneficial when you need to create a simple, well-defined pattern, but it may not offer the same level of flexibility as `new RegExp()`. **Library: String.prototype.split()** The `split()` method is a built-in JavaScript method that splits a string into an array of substrings based on a regular expression. The library used here is the native JavaScript library. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. **Alternatives** If you're interested in exploring alternative approaches for splitting strings, consider using: * `Array.prototype.reduce()`: Instead of using `split()` and concatenating arrays, you can use `reduce()` to build the output array. * `String.prototype.replaceAll()`: This method replaces all occurrences of a pattern with another value, which can be useful for string manipulation tasks. * Custom implementations: Depending on your specific requirements, you might consider implementing your own custom solution using loops or other algorithms. Keep in mind that these alternatives may offer different performance characteristics and trade-offs compared to the `split()` method.
Related benchmarks:
path regex bis
path regex bis 2
Various regex testing patterns
pippoepluto
Comments
Confirm delete:
Do you really want to delete benchmark?