Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes() vs String.includes()
(version: 0)
Comparing performance of:
Array.includes() vs String.includes()
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ids = Array(100).fill().map((_, i) => ++i); var serializedIds = `,${ids.join()},`; var searchIds = [1, 50, 100, 150]; var searchSerializedIds = searchIds.map(id => `,${id},`);
Tests:
Array.includes()
searchIds.map(id => ids.includes(id));
String.includes()
searchSerializedIds.map(id => serializedIds.includes(id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes()
String.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.1:latest
, generated one year ago):
Let's dive into the explanation of the benchmark test case. **Benchmark Name:** Array.includes() vs String.includes() **Description:** This benchmark compares the performance of two JavaScript methods for searching within arrays and strings: 1. `Array.prototype.includes()` method, which is used to search for an element in an array. 2. The built-in `String.prototype.includes()` method, which searches for a substring within a string. **Test Case Preparation Code:** The preparation code creates three variables: * `ids`: An array of 100 integers (1 to 100) created using `Array(100).fill().map((_, i) => ++i);`. * `serializedIds`: A string representation of the `ids` array, where each element is separated by a comma. * `searchIds`: An array of specific IDs (1, 50, 100, and 150) that will be used for searching within the `ids` array. * `searchSerializedIds`: An array of strings representing the `searchIds` elements, with each ID enclosed in commas. **Individual Test Cases:** There are two test cases: 1. **Array.includes()**: The test case uses the `map()` method to iterate over the `searchIds` array and search for each element within the `ids` array using `Array.prototype.includes()`. 2. **String.includes()**: The test case uses the `map()` method to iterate over the `searchSerializedIds` array and search for each string within the `serializedIds` string using the built-in `String.prototype.includes()` method. **Latest Benchmark Result:** The result shows the execution per second (EPS) for both test cases on Chrome 107: * **Array.includes()**: 1,921,628.5 EPS * **String.includes()**: 424,347.8125 EPS **Comparison and Conclusion:** The results indicate that searching within an array using `Array.prototype.includes()` is significantly faster than searching within a string using the built-in `String.prototype.includes()` method. **Pros/Cons of Each Approach:** * **Array.includes()**: + Pros: - Faster execution time (1,921,628.5 EPS) - Suitable for searching arrays with a large number of elements + Cons: - Requires an array to search within * **String.includes()**: + Pros: - Easy to use and understand + Cons: - Slower execution time (424,347.8125 EPS) - Not suitable for searching large arrays **Other Considerations:** When choosing between `Array.includes()` and `String.includes()`, consider the type of data you're working with: * Use `Array.includes()` when searching within an array is more efficient and convenient. * Use `String.includes()` when searching within a string or concatenating strings. **Library:** None There are no external libraries used in this benchmark test case.
Related benchmarks:
equality vs includes
chain of or equals vs includes but smaller
=== vs includes
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?