Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs. String functions v2
(version: 0)
Comparing performance of:
String functions vs Regex
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var contentType = 'application/problem+json; charset=utf-8';
Tests:
String functions
(_.includes(contentType, 'application/json') || (_.startsWith(contentType, 'application/') && _.includes(contentType, '+json')))
Regex
contentType.match(/application\/json|.*\+json/);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String functions
Regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String functions
5127396.5 Ops/sec
Regex
5098099.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **What is being tested?** The benchmark tests two approaches to parse a string that represents a MIME type: using regular expressions (`Regex`) versus using string functions (`String functions`). **Options compared:** 1. **Regular Expressions (Regex)**: * Purpose: To match a specific pattern in the string. * Pros: + Highly flexible and powerful for complex patterns. + Can be used to extract multiple values from a single string. * Cons: + Can be slower due to the overhead of compiling and executing regex patterns. + May have performance issues if not optimized properly. 2. **String Functions**: * Purpose: To use built-in JavaScript functions that operate on strings (e.g., `includes()`, `startsWith()`). * Pros: + Typically faster than Regex, as it avoids the overhead of compiling and executing regex patterns. + Easier to read and maintain for simple string operations. **Other considerations:** 1. **Library usage**: The benchmark uses Lodash's `_.includes()` function in one of the test cases. Lodash is a popular utility library that provides a collection of functional programming helpers, including string operations. Its use adds another layer of complexity to the benchmark. 2. **JavaScript features and syntax**: The benchmark does not rely on any special JavaScript features or syntax, making it accessible to a wide range of developers. **Alternative approaches:** Other methods for parsing MIME type strings could include: 1. **Using `match()` with a string literal**: Similar to the Regex approach, but without the overhead of compiling and executing a regex pattern. 2. **Using a custom implementation**: Writing a custom function to parse the MIME type string, which may be more efficient than using built-in JavaScript functions or regex. **Benchmark preparation code:** The `Script Preparation Code` section sets an HTTP header for the benchmark content type, while the `Html Preparation Code` includes Lodash's script tag to enable the use of its `_.includes()` function in the tests.
Related benchmarks:
Array.find vs lodash _.includes vs compiled regex
Array.prototype.find vs _.find vs regex
Lodash trim VS native regexp
Regex vs. String functions
Comments
Confirm delete:
Do you really want to delete benchmark?