Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array of strings | Some vs Includes (+ spread)
(version: 1)
Comparing performance of:
Some vs Includes vs Includes + spread (when TypeScript readonly)
Created:
10 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
// Preparation: create an array of 10 string values const values = [ "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliet" ]; // Function to pick a random string from the array function getRandomValue() { const index = Math.floor(Math.random() * values.length); return values[index]; }
Tests:
Some
const target = getRandomValue(); const found = values.some(v => v === target);
Includes
const target = getRandomValue(); const found = values.includes(target);
Includes + spread (when TypeScript readonly)
const target = getRandomValue(); const found = [...values].includes(target);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Some
Includes
Includes + spread (when TypeScript readonly)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
12 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 Edg/149.0.0.0
Browser/OS:
Chrome 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Some
43353412.0 Ops/sec
Includes
36411896.0 Ops/sec
Includes + spread (when TypeScript readonly)
34967132.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
This benchmark tests the performance of different methods for finding a string in an array of strings in JavaScript. The options compared are: 1. **Using `Array.prototype.some()`**: - **Test Case**: `const found = values.some(v => v === target);` - **Description**: The `some()` method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value. - **Pros**: - More readable and expressive, as it conveys the intent of checking for existence based on a condition. - Can accept a predicate function for more complex checks. - **Cons**: - Slightly slower than `includes()` in many cases since the callback function is invoked for each element until a match is found. 2. **Using `Array.prototype.includes()`**: - **Test Case**: `const found = values.includes(target);` - **Description**: The `includes()` method determines whether an array includes a certain value among its entries, returning a Boolean value. - **Pros**: - More straightforward when checking for the presence of a specific value, as it doesn't require a function. - Generally faster than `some()` since there is no function call overhead. - **Cons**: - Less flexible, as it can only check for direct equality, not for more complex conditions. 3. **Using `includes()` with the spread operator**: - **Test Case**: `const found = [...values].includes(target);` - **Description**: This approach uses the spread operator (`...`) to create a shallow copy of the `values` array before calling `includes()`. - **Pros**: - Useful when dealing with certain scenarios in TypeScript where an array may be readonly. - **Cons**: - Introduces an overhead due to the copying of the array, making it generally less efficient than the plain `includes()`. - More memory intensive since it creates a new array. ### Performance Results The benchmark shows how many times per second each test case was executed: - **Some**: 24,240,238 executions per second - **Includes**: 20,410,406 executions per second - **Includes + spread**: 16,681,489 executions per second ### Conclusion From this benchmark, `Array.prototype.some()` performed the best in terms of execution speed. However, the usage of `includes()` offers clarity and efficiency for simple value existence checks. The spread operator's addition provides a workaround for specific situations (like TypeScript readonly arrays) but at a performance cost. ### Other Alternatives - **Linear Search**: Implementing a manual loop to iterate through the array and check for equality can be another alternative, but is generally less readable and more error-prone than using built-in methods. - **Set or Map**: If checking for existence is a frequent operation for large datasets, converting the array into a Set can significantly enhance performance for lookups (constant time complexity). - **Binary Search**: If the array is sorted, binary search can be considered for improved performance; however, it requires the array to be sorted and is more complex to implement.
Related benchmarks:
Text.data vs Text.nodeValue
Stor info in Array vs Object
Split vs Spread (randomized)
generateRandomString
String character looping
Measure speed
testing array ops
endsWith vs at vs trimEnd vs Regex
Array of strings | Some vs Includes
Comments
Confirm delete:
Do you really want to delete benchmark?