Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes() in an array vs includes() in a string 3
(version: 0)
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()},`;
Tests:
Array.includes()
ids.includes(1); ids.includes(20000); ids.includes(40000); ids.includes(60000);
String.includes()
serializedIds.includes(',1,'); serializedIds.includes(',20000,'); serializedIds.includes(',40000,'); serializedIds.includes(',60000,');
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 Overview** The benchmark is comparing two different approaches to search for an element in arrays and strings: 1. `Array.includes()` (also known as "Array.prototype.includes()") 2. `String.includes()` (a method introduced in ECMAScript 2015, also known as "String.prototype.includes()") The goal is to determine which approach is faster. **Script Preparation Code** This code generates an array of 40,000 elements and shuffles it using the Fisher-Yates algorithm. This ensures that the array is randomly ordered, making it a good test case for the `includes()` method. ```javascript 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)); ``` **Html Preparation Code** This is empty, indicating that no HTML code needs to be executed before running the benchmark. **Individual Test Cases** There are two test cases: 1. `Array.includes()`: This test case uses the `includes()` method on the shuffled array `ids`. The test case searches for elements at indices 1, 20,000, and 40,000. 2. `String.includes()`: This test case uses the `includes()` method on a string containing all the IDs from the `ids` array, separated by commas. ```javascript serializedIds = `,${ids.join()},`; ``` **Benchmark Results** The provided results show that Chrome 107 on Linux outperforms the `Array.includes()` method significantly. However, it's essential to note that this result may not be representative of other browsers or devices. Pros and Cons of different approaches: * **`Array.includes()`**: This method iterates over the array until finding the element or reaching the end. It has a time complexity of O(n), where n is the size of the array. * Pros: + Widely supported across browsers and platforms + Can be used with various data types (arrays, objects) * Cons: + May have performance issues for large datasets * **`String.includes()`**: This method iterates over the string character by character until finding the element or reaching the end. It has a time complexity of O(n), where n is the length of the string. * Pros: + Optimized for searching strings, which are often larger than arrays + May be faster for search-intensive tasks * Cons: + Less widely supported across browsers and platforms compared to `Array.includes()` **Library Usage** In this benchmark, no libraries other than JavaScript's built-in functionality is used. **Special JS Features or Syntax** The benchmark uses the ECMAScript 2015 feature of the `includes()` method for strings. It also leverages modern JavaScript features such as template literals and array methods like `fill()` and `map()`.
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?