Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Large Array Case-Insensitive String Filtering
(version: 0)
Test various case insensitive string comparison methods inside large (1000 item) arrays.
Comparing performance of:
toLowerCase and indexOf vs regex case-insensitive flag
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
toLowerCase and indexOf
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['Bingo', 'BINGO', 'bingo', 'Bingo', 'bingo']); array.filter(i => i.toLowerCase().indexOf('bingo') !== -1);
regex case-insensitive flag
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['Bingo', 'BINGO', 'bingo', 'Bingo', 'bingo']); array.filter(i => i.match(/bingo/i));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
toLowerCase and indexOf
regex case-insensitive flag
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 the provided JSON benchmark. **Benchmark Definition** The benchmark is defined by a script that creates an array of 1000 items, where some of them contain a string "Bingo" or its variations (in different cases: uppercase "BINGO", lowercase "bingo"). The script then filters this array to find all items containing the substring "bingo". **Options Compared** Two approaches are being compared: 1. **`toLowerCase()` and `indexOf`**: This approach uses the `toLowerCase()` method to convert all strings to lowercase before filtering them for the presence of "bingo". The `indexOf` method is then used to find the index of "bingo" in each string. 2. **Regex case-insensitive flag** (`/bingo/i`): This approach uses a regular expression that includes the `i` flag, which makes the match case-insensitive. **Pros and Cons** 1. **`toLowerCase()` and `indexOf`**: * Pros: Easy to understand, widely supported by most browsers. * Cons: May not be as performant for very large arrays or when working with a lot of different data types (e.g., strings, numbers). 2. **Regex case-insensitive flag** (`/bingo/i`): * Pros: More efficient than `toLowerCase()` and `indexOf`, especially for large datasets. * Cons: Requires knowledge of regular expressions, which can be challenging to understand and implement. **Library Usage** There is no explicit library usage mentioned in the benchmark definition or individual test cases. However, it's worth noting that some JavaScript engines (e.g., V8 in Chrome) optimize certain string operations, including case-insensitive matching, using built-in libraries or internal algorithms. **Special JS Feature/Syntax** None of the provided code uses any special JavaScript features or syntax beyond regular expressions and array methods. **Other Alternatives** If you want to explore other approaches for case-insensitive string comparison: * **`match()` with a regex**: Similar to the second approach, but without using `i` flag (e.g., `/bingo/`). * **`includes()`**: A newer method introduced in ECMAScript 2019, which can be used for case-insensitive matching. However, this might not be supported by older browsers. * **Custom implementation**: You could implement your own case-insensitive matching algorithm using bitwise operations or other techniques. Keep in mind that the performance of these alternatives may vary depending on the specific use case and JavaScript engine being used.
Related benchmarks:
indexOf vs includes vs some - 20211114
array indexOf vs includes vs some long list
array indexOf vs includes vs some vs binary search on large array
lodash intersectionWith vs array filtering with includes and map
array indexOf vs includes vs some w/ largeish array
Comments
Confirm delete:
Do you really want to delete benchmark?