Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
check placeholder in string
(version: 1)
Test check placeholder in string, for avoid a place holder is injected into log
Comparing performance of:
String.includes vs Regex
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
var input = 'abcd %{public}s efg';
Script Preparation code:
var input = 'abcd %{public}s efg'; var templateStrs = ['%{private}s', '%{private}d', '%{public}s', '%{public}d']; var regPattern = /%\{(public|private)\}[sd]/;
Tests:
String.includes
let hasPlaceholder = false; for (let i = 0; i < 4; i++) { if (input.includes(templateStrs[i])) { hasPlaceholder = true; break; } }
Regex
let hasPlaceholder = false; hasPlaceholder = regPattern.test(input);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String.includes
Regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.includes
2473322.8 Ops/sec
Regex
7897303.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you're looking at measures the performance of two different approaches to check for the presence of placeholders in a string. The goal is to determine if the string contains certain specified template placeholders, specifically `%{public}s`, `%{private}s`, `%{private}d`, and `%{public}d`. ### Test Cases Explained 1. **Using `String.includes`**: ```javascript let hasPlaceholder = false; for (let i = 0; i < 4; i++) { if (input.includes(templateStrs[i])) { hasPlaceholder = true; break; } } ``` - **Description**: This approach iterates over an array of template strings and checks if the input string contains any of those template strings using the `String.includes()` method. - **Pros**: - **Simplicity**: The code is straightforward and easy to understand. - **Readability**: By explicitly checking each string, it's clear what the code is doing. - **Cons**: - **Performance**: This method can be slower because it potentially checks each string one by one and iterates through the array. - **Scalability**: For a greater number of template strings, the time complexity increases linearly. 2. **Using Regular Expression**: ```javascript let hasPlaceholder = false; hasPlaceholder = regPattern.test(input); ``` - **Description**: This method uses a regular expression to test if the input string matches the defined pattern, which looks for the placeholders `%{public}` or `%{private}` followed by either `s` (string) or `d` (number). - **Pros**: - **Performance**: Generally faster for matching patterns directly in a string, especially as the size of the input grows. - **Conciseness**: This approach requires much less code compared to iterating through possible strings. - **Cons**: - **Complexity**: Regular expressions can be harder to read and understand for those less familiar with regex syntax. - **Overhead for Simple Matching**: If the only purpose is checking a small set of static strings, using regex may introduce unnecessary complexity. ### Library and Features Neither test case employs an external library; rather, they utilize built-in JavaScript functionality—specifically, the `String.includes()` method and the `RegExp.test()` method. ### Performance Results From the benchmark results: - The Regex test has a higher execution rate at **7,897,303.5 executions per second**. - The `String.includes` approach registered **2,473,322.75 executions per second**. This clearly indicates that the Regex approach was significantly more performant in this scenario. ### Other Alternatives - **Using `Array.prototype.some()` with `String.includes()`**: Instead of a loop, one could use `some()` to improve readability while still checking against the array. This could provide a clearer intent to other developers. ```javascript const hasPlaceholder = templateStrs.some((str) => input.includes(str)); ``` - **String Search**: For large strings and if the patterns are fixed, one can explore specific string search algorithms (e.g., Boyer-Moore) implemented via custom functions. However, this would typically outperform regex only in specific use cases. Ultimately, the choice between these approaches should consider the size of the input, the number of placeholders, and how often this check needs to be performed—while also factoring in the importance of code readability versus performance.
Related benchmarks:
string comparation
format string
Compare string place methods 2
match v includes
test if/else and if/else-if 2
Check boolean
+= vs templateString ...
String falsy test
check vs replace 3
Comments
Confirm delete:
Do you really want to delete benchmark?