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);
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['Bingo', 'BINGO', 'bingo', 'Bingo', 'bingo']);
array.filter(i => i.match(/bingo/i));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
toLowerCase and indexOf | |
regex case-insensitive flag |
Test name | Executions per second |
---|---|
toLowerCase and indexOf | 26494.6 Ops/sec |
regex case-insensitive flag | 27489.2 Ops/sec |
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:
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./bingo/i
): This approach uses a regular expression that includes the i
flag, which makes the match case-insensitive.Pros and Cons
toLowerCase()
and indexOf
:/bingo/i
):toLowerCase()
and indexOf
, especially for large datasets.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.Keep in mind that the performance of these alternatives may vary depending on the specific use case and JavaScript engine being used.