Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
match vs include vs indexOf
(version: 0)
Comparing performance of:
match vs include vs indexof
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var badWords = ['xxx', 'vaffanculo', 'merda', 'cazzo', 'figa', 'culo', 'dio', 'porco', 'porca', 'coglione', 'pirla', 'deficente', 'bastardo', 'stronzo', 'puttana', 'troia', 'frocio', 'cacca', 'zoccola', 'bagascia', 'cane', 'madre', 'padre', 'porco dio', 'procodio', 'porca madonna', 'madonna puttana', 'cristo', 'gesu', 'berlusconi', 'obama', 'trump', 'paperino', 'pippo', 'pluto', 'paperone', 'tizio', 'caio', 'sempronio', 'cip', 'ciop', 'totti', 'del piero', 'ronaldo', 'paperon', 'paperoni', 'disney', 'pinco', 'pallino', 'zio', 'zia', 'ciao', 'cane', 'topo', 'test', 'prova', 'proviamo', 'nome', 'cognome',] var badWordsRegex = new RegExp(`(${badWords.map(s => `\\b${s}\\b`).join('|')})`, 'i')
Tests:
match
"cazzo".match(badWordsRegex)
include
badWords.includes("cazzo")
indexof
badWords.indexOf("cazzo") > -1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
match
include
indexof
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
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
match
14623932.0 Ops/sec
include
19895470.0 Ops/sec
indexof
19837002.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark defines three test cases: `match`, `include`, and `indexOf`. These test cases aim to measure the performance of different string matching approaches in JavaScript. **Test Cases** 1. **`match`**: This test case uses the `.match()` method to search for a specific substring (`"cazzo"`). The method returns an array with the matched value(s) or null if no match is found. 2. **`include`**: This test case uses the `.includes()` method to check if a specific substring (`"cazzo"`) exists within the `badWords` array. 3. **`indexOf`**: This test case uses the `.indexOf()` method to search for a specific index value (the position of `"cazzo"` in the `badWords` array). **Regexp Creation** The benchmark creates a regular expression (`RegExp`) that matches any of the words in the `badWords` array. The `map()` function is used to escape each word with a `\b`, which ensures that word boundaries are respected. **Comparison and Pros/Cons** The three test cases are designed to measure the performance differences between: * `.match()`: A general-purpose string matching method that can be slow due to its regular expression engine usage. + Pros: Can match multiple patterns, supports named groups, and is easy to use. + Cons: Slow due to regex engine overhead, may not perform well on large strings or complex patterns. * `.includes()`: An array method that checks if a specific value exists within the array. + Pros: Fast, lightweight, and easy to use. Only iterates over the relevant elements in the array. + Cons: May be slower for very large arrays, only supports exact matches. * `.indexOf()`: A method that searches for a specific index value within an array or string. + Pros: Fast, lightweight, and easy to use. Only iterates over the relevant elements in the array. + Cons: Slow if the element is not found (returns -1), only supports exact matches. **Library and Purpose** * `RegExp`: A built-in JavaScript library for creating regular expressions. * `Array.prototype.includes()`: An array method that checks if a specific value exists within the array. Introduced in ECMAScript 2015. * `Array.prototype.indexOf()`: An array method that searches for a specific index value within the array. Introduced in ECMAScript 1999. **Other Considerations** * The benchmark measures the number of executions per second (ExecutionsPerSecond) for each test case, which is a common metric for performance testing. * The results are likely compared to determine the fastest approach, but other factors like memory usage or overhead might also be relevant depending on the specific use case. **Alternatives** Other alternatives could include: * Using a different string matching library or module, such as `string-pose` or `fast-match`. * Implementing custom string matching logic using techniques like Boyer-Moore or Knuth-Morris-Pratt algorithms. * Testing with more complex input patterns or larger datasets to further evaluate performance differences. Keep in mind that the specific choice of testing framework and alternatives depends on the use case, requirements, and desired outcome.
Related benchmarks:
Unique via Set vs Filter
match vs include
test vs include vs indexOf
test vs include vs indexOf no match
Comments
Confirm delete:
Do you really want to delete benchmark?