var issues = [{
name: "Take trash out",
id: 1,
state: "open"
},
{
name: "Make a laundry",
id: 2,
state: "open"
},
{
name: "Meet a friend in KFC",
id: 3,
state: "open"
}
];
var search = "take";
var regexp = new RegExp(search,'i');
issues.filter(function(issue) {return regexp.test(issue.name)});
issues.filter(function(issue) {return issue.name.toLowerCase().includes(search.toLowerCase())});
issues.filter(function(issue) { return issue.name.toLowerCase().match(search.toLowerCase())});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
String.includes | |
String.match |
Test name | Executions per second |
---|---|
RegEx.test | 5546115.5 Ops/sec |
String.includes | 4310142.0 Ops/sec |
String.match | 1982271.4 Ops/sec |
The benchmark provided measures the performance of three different string search methods in JavaScript: regular expressions (RegExp.test
), String.includes
, and String.match
. Each method is applied to a list of issues (string objects) that represent tasks, where the goal is to find matches based on a search term ("take") in the task name.
RegExp.test
issues.filter(function(issue) { return regexp.test(issue.name); });
name
property of each issue. The regular expression regexp
is constructed to be case-insensitive due to the 'i' flag.String.includes
issues.filter(function(issue) { return issue.name.toLowerCase().includes(search.toLowerCase()); });
includes
method checks if a given string (search term) is contained within another string (in this case, the name
of an issue). Both strings are converted to lowercase to perform a case-insensitive comparison.String.match
issues.filter(function(issue) { return issue.name.toLowerCase().match(search.toLowerCase()); });
match
method is utilized here to find a match for the search term against the issue name as a regular expression. It also uses case-insensitive matching by converting strings to lowercase; however, it doesn't create a new regex for each invocation.The benchmark results show the number of times each method can execute per second on a specific Chrome browser environment:
RegExp.test
: 5,546,115.5 executions/secondString.includes
: 4,310,142.0 executions/secondString.match
: 1,982,271.375 executions/secondRegExp.test
String.includes
String.match
String.includes
is appropriate, whereas RegExp.test
is better for more complex patterns or when performance is critical.RegExp
, performance can degrade; hence, it’s important to balance complexity and performance.In addition to these methods, other alternatives for string searching include:
String.indexOf: This method returns the index of the first occurrence of a specified value in a string. It can also be used for existence checks but is less expressive than includes
.
Array.prototype.some: This method can combine any of the above functionalities and is useful if you want to short-circuit on the first match found, which can improve performance in certain scenarios.
Third-party Libraries: Libraries like Lodash or Underscore may offer utility functions that perform string searches that can be optimized for specific tasks.
Overall, understanding the use cases, performance implications, and trade-offs of each method is essential for making informed decisions in JavaScript string manipulation tasks.