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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of **`Array.includes()`** versus **`String.includes()`** for searching values within data structures. Here's a breakdown: * **What's being compared:** The benchmark compares two different methods for finding if a value exists within a collection: * **`Array.includes()`:** This is a built-in JavaScript method designed specifically to check if an element is present in an array. * **`String.includes()`:** This method checks if a substring exists within a string. However, in this benchmark, it's being used somewhat creatively by converting the array into a comma-separated string (`serializedIds`) and then using `String.includes()` to search for the values within that string representation. * **Pros & Cons:** * **`Array.includes()`:** * **Pro:** Optimized for searching arrays, generally faster. * **Con:** Only works with arrays; not directly applicable to other data types like strings. * **`String.includes()`:** * **Pro:** Can be used on strings, which might offer a workaround in some specific scenarios. * **Con:** Less efficient for searching within arrays compared to `Array.includes()`. In this case, the conversion to and from string format adds overhead, making it significantly slower. * **Alternatives:** * Other JavaScript array methods like `indexOf()` or filtering (`filter()`) could also be used for value search operations in arrays. The best alternative depends on the specific use case and desired outcome. Let me know if you have any more questions!
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?