Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs. String functions
(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 = 'foo';
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. The test is comparing two approaches to check if the string `contentType` contains the substring `'application/json'`. **Approach 1: Using Lodash (String functions)** ```javascript (_.includes(contentType, 'application/json') || (_.startsWith(contentType, 'application/') && _.includes(contentType, '+json'))) ``` This approach uses the Lodash library to perform string operations. Specifically: * `_.includes()` checks if a value is included in an array (in this case, `contentType`). * `_.startsWith()` checks if a value starts with a specified prefix. The pros of using Lodash are that it provides a simple and concise way to perform common string operations. However, this approach may be slower than native JavaScript methods for large strings or performance-critical code. **Approach 2: Using Regex (Regex)** ```javascript contentType.match(/application\/(json|.*\+json)/) ``` This approach uses a regular expression to match the desired substring. The regex pattern: * `^` matches the start of the string. * `/application/` matches the literal string "application/". * `(json|.*\+json)` is a capturing group that matches either "json" or any string starting with "+json". The pipe (`|`) character is used to match either one of the patterns. The pros of using Regex are that it can be more efficient than string functions, especially for larger strings. However, Regex can be error-prone and may require additional maintenance if the pattern changes. **Other considerations:** * Both approaches assume that `contentType` is a string containing the desired substring. * The benchmark measures the number of executions per second (ExecutionsPerSecond) to determine which approach is faster. * Other factors like browser version, device platform, and operating system may affect the performance of these tests. **Lodash library:** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array operations, and functional programming. In this test, Lodash is used to perform string operations like `includes()` and `startsWith()`.
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 v2
Comments
Confirm delete:
Do you really want to delete benchmark?