Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string includes vs foreach (split)
(version: 0)
Comparing performance of:
string includes vs string foreach (split)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = "string 1234" var arr2 = ["A","B","C","D","a","b","c"]
Tests:
string includes
arr2.map(x => { if (arr1.includes(x)) x.disabled = true; return x; });
string foreach (split)
arr2.map(x => { arr1.split("").forEach(y => { if (x.value === y) x.disabled = true; }); return x; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
string includes
string foreach (split)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
string includes
10423830.0 Ops/sec
string foreach (split)
4444236.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark test case created using MeasureThat.net. The test compares the performance of two approaches for checking if an element exists in an array: `includes` method and `forEach` loop with string splitting. **Options Compared** There are two options being compared: 1. **`includes` Method**: This method checks if a specified value (in this case, a string) exists in the array using a linear search algorithm. 2. **`forEach` Loop with String Splitting**: This approach uses the `forEach` loop to iterate over each element of the array and checks if the string matches any of the elements using the `split()` method. **Pros and Cons** 1. **`includes` Method**: * Pros: Simple, efficient, and widely supported across browsers. * Cons: Linear search algorithm can be slow for large arrays, and it only works with primitive values (strings, numbers). 2. **`forEach` Loop with String Splitting**: * Pros: Can handle arrays of strings, allows for custom filtering logic, and can be more efficient than `includes` for large datasets. * Cons: More complex implementation, may require additional computations (e.g., splitting strings), and not as widely supported across browsers. **Library Used** None. The test uses built-in JavaScript methods (`includes`, `forEach`) without any external libraries. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. It only employs standard JavaScript methods and concepts. **Benchmark Preparation Code** The provided Script Preparation Code initializes two variables: * `arr1`: a string containing the value "string 1234" (used for matching with strings in `arr2`) * `arr2`: an array of strings ["A", "B", "C", "D", "a", "b", "c"] (used as the source array for benchmarking) **Other Alternatives** If you wanted to create a similar benchmark, you could use other approaches to check if an element exists in an array, such as: * Using `indexOf()` method * Implementing a custom binary search algorithm * Utilizing modern JavaScript features like `Set` data structures or `findIndex()` * Employing optimized string matching algorithms (e.g., Boyer-Moore algorithm) Keep in mind that the best approach will depend on your specific use case and performance requirements.
Related benchmarks:
JSON.parse vs string.split small fixed array
Array split vs string substring ISO String
JSON.parse vs string.split 2
Array split vs string substring22
JSON.parse vs string.splitds
Comments
Confirm delete:
Do you really want to delete benchmark?