Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Text search performance
(version: 2)
Comparing various text search methods
Comparing performance of:
Includes vs Regex vs Search vs Match vs IndexOf vs Lodash Includes
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div>Tests</div> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function generateItems() { let items = []; for (let i = 0; i < 60000; i++) { items.push({ value: 'test' + Math.random() }); } return items; }
Tests:
Includes
let items = generateItems(); let filteredItems = items.filter(item => item.value.includes('test0.5'));
Regex
let items = generateItems(); let filteredItems = items.filter(item => /test0\.5/.test(item.value));
Search
let items = generateItems(); let filteredItems = items.filter(item => item.value.search(/test0\.5/) >= 0);
Match
let items = generateItems(); let filteredItems = items.filter(item => Boolean(item.value.match(/test0\.5/)));
IndexOf
let items = generateItems(); let filteredItems = items.filter(item => item.value.indexOf('test0.5') >= 0);
Lodash Includes
let items = generateItems(); let filteredItems = items.filter(item => _.includes(item.value, 'test0.5'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Includes
Regex
Search
Match
IndexOf
Lodash Includes
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):
I'll break down the benchmark and its various test cases, explaining what's being tested, the pros and cons of each approach, and any relevant libraries or special features used. **Benchmark Overview** The benchmark is designed to compare the performance of different text search methods. The script generates an array of 60,000 objects with a random string value, and then filters the array using various methods. The goal is to find the fastest method for filtering an array of strings that contains a specific pattern or substring. **Test Cases** Here's a brief explanation of each test case: 1. **Includes**: Uses the `includes()` method to check if the string contains the specified pattern. * Pros: Simple and intuitive, widely supported by modern browsers. * Cons: May be slower than other methods for large arrays or complex patterns. 2. **Regex**: Uses a regular expression to search for the pattern in the string. * Pros: Highly flexible and powerful, can match complex patterns. * Cons: Can be slow for simple searches or large arrays. 3. **Search**: Uses the `search()` method to find the index of the first occurrence of the pattern in the string. * Pros: Faster than `includes()` for very long strings or performance-critical applications. * Cons: May not work as expected if the pattern is not found, can be slower for small arrays. 4. **Match**: Uses the `match()` method to search for a full match of the pattern in the string. * Pros: Similar to `includes()`, but returns an array with the matched string. * Cons: May be slower than other methods for large arrays or complex patterns. 5. **IndexOf**: Uses the `indexOf()` method to find the index of the first occurrence of the substring. * Pros: Fast and efficient, widely supported by modern browsers. * Cons: Only returns the index of the first occurrence, may not work as expected if the pattern is not found. 6. **Lodash Includes**: Uses the `includes()` function from the Lodash library to check if an element is in an array. * Pros: Similar to native `includes()`, but provides additional utility functions and better performance for large arrays. * Cons: Requires including the Lodash library, which may add overhead. **Library and Special Features** The benchmark uses the following libraries: 1. **Lodash**: A popular JavaScript library that provides a wide range of utility functions, including `includes()`. The Lodash library is included in the HTML preparation code. The benchmark does not use any special JavaScript features or syntax beyond what's supported by modern browsers. **Other Alternatives** If you're looking for alternative text search methods or want to explore other approaches, consider the following: 1. **Substring searching**: Use a simple substring search algorithm, such as iterating through the string and checking if the pattern matches at each position. 2. **Bloom filters**: Use a Bloom filter data structure to quickly determine if an element is present in the array without actually filtering it. 3. **Hash-based filtering**: Use a hash function to map the string values to indices in an array, then use a binary search algorithm to find matching elements. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the methods tested in the benchmark.
Related benchmarks:
Array.prototype.find vs Lodash find
Array.prototype.find vs Lodash find 2
Lodash vs Native Find
find vs lodash find
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
Comments
Confirm delete:
Do you really want to delete benchmark?