Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test vs include vs indexOf
(version: 0)
Comparing performance of:
test 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:
test
badWordsRegex.test("cazzo")
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
test
include
indexof
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):
**Benchmark Overview** The provided benchmark measures the performance of three different approaches for searching an array of strings: `includes()`, `indexOf()`, and `test()` with a regular expression. The test case uses an array of profanity words, which are iterated over to search for specific words. **Options Compared** 1. **`includes()`**: This method checks if a word is present in the array using the `===` operator. 2. **`indexOf()`**: This method returns the index of the first occurrence of a word in the array (or -1 if not found). 3. **`test()` with regular expression**: This method uses a regular expression to search for a specific pattern in the string. **Pros and Cons** * `includes()`: Pros: simple, efficient, and widely supported. Cons: may not be suitable for large datasets or complex searches. * `indexOf()`: Pros: can return an index value, which can be useful in certain contexts. Cons: may have a higher overhead than `includes()` due to the function call and potential string concatenation. * `test()` with regular expression: Pros: flexible, efficient, and suitable for complex searches. Cons: requires a regular expression expertise and may not be as straightforward to implement. **Regular Expression Library** The test case uses the `RegExp` object to create a regular expression that matches any of the profanity words in the array. The regular expression is created using the `map()` function, which replaces each word with a escaped version (`\\b<word>\\b`) and concatenates them using the pipe character (`|`). This creates a regular expression pattern that matches any of the individual words. **Special JavaScript Features** This benchmark uses the following special JavaScript features: * Regular expressions: The `test()` method is used in conjunction with a regular expression to search for specific patterns. * `map()`: The `map()` function is used to create an array of escaped profanity words, which are then used to construct the regular expression. **Alternative Approaches** Other approaches that could be used to implement this benchmark include: * `forEach()`: Instead of using `includes()` or `indexOf()`, a custom implementation could use `forEach()` to iterate over the array and check for each word. * `Array.prototype.some()`: Similar to `includes()`, but with a more concise syntax. * Custom implementation: A custom implementation using native JavaScript functions, such as `String.prototype.indexOf()` or `Array.prototype.indexOf()`, could be used instead of relying on built-in methods. Overall, this benchmark provides a good comparison between three common approaches for searching an array of strings in JavaScript. The use of regular expressions adds an additional layer of complexity and flexibility to the test case.
Related benchmarks:
IndexOf vs Includes vs lodash includes on the arrays of strings
match vs include
match vs include vs indexOf
test vs include vs indexOf no match
Comments
Confirm delete:
Do you really want to delete benchmark?