Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes() in an array vs includes() in a string 2
(version: 1)
Comparing performance of:
Array.includes() vs String.includes()
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const shuffle = ([...arr]) => { let m = arr.length; while (m) { const i = Math.floor(Math.random() * m--); [arr[m], arr[i]] = [arr[i], arr[m]]; } return arr; }; var ids = shuffle(Array(40000).fill().map((_, i) => ++i)); var serializedIds = `,${ids.join()},`; var searchIds = [1, 20000, 40000, 60000]; 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.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The test is comparing the performance of two approaches: 1. `includes()` on an array (`Array.includes()`). 2. `includes()` on a string (`String.includes()`). In JavaScript, both methods are implemented in the browser engine (V8) and use similar algorithms to find the index of a specified element within the array or string. **Options Compared** The two options being compared are: 1. **Array includes()**: This method is called on an array, which is a native JavaScript data structure. It's optimized for searching and manipulation of arrays. 2. **String includes()**: This method is called on a string, which is also a native JavaScript data structure. However, strings have additional overhead due to the need to handle character encoding and Unicode. **Pros and Cons** Here are some pros and cons of each approach: * **Array includes():** + Pros: - Optimized for array search - Less overhead compared to string search + Cons: - Limited to searching arrays, not strings or other data structures * **String includes():** + Pros: - Can be used on any string, not just native JavaScript strings - May perform better for certain types of string searches (e.g., regex) + Cons: - Additional overhead due to character encoding and Unicode handling **Library Usage** There is no explicit library usage in the provided JSON. However, it's worth noting that V8, the JavaScript engine used by Chrome, has its own internal optimization techniques for array and string search. **Special JS Features/Syntax** The benchmark doesn't explicitly use any special JavaScript features or syntax beyond native JavaScript data structures (arrays and strings). If it did, I'd be happy to explain those in more detail. **Other Alternatives** If you're interested in exploring alternative approaches or libraries for array and string search, some options might include: * Using a third-party library like Lodash or Ramda for functional programming and array manipulation * Implementing custom algorithms using native code (e.g., C++ or Assembly) for optimal performance * Utilizing specialized data structures like suffix trees or Aho-Corasick automata for efficient string search Keep in mind that these alternatives might introduce additional complexity, dependencies, or performance overhead.
Related benchmarks:
Array find with indexOf vs includes
equality vs includes
=== vs includes
equals vs includes
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?