Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String.includes vs Array.includes sorted
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
Array.includes vs String.includes vs Array.includes sorted
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = "org:accounting:manage,org:admin:manage,org:customerservice:manage,org:franchisesales:manage,org:sales:manage,org:telemarketing:manage".split(","); var testArraySorted = "org:accounting:manage,org:admin:manage,org:customerservice:manage,org:franchisesales:manage,org:sales:manage,org:telemarketing:manage".split(",").sort(); var testString = "org:accounting:manage,org:admin:manage,org:customerservice:manage,org:franchisesales:manage,org:sales:manage,org:telemarketing:manage";
Tests:
Array.includes
const testStrFirst = 'org:sales:manage'; testArray.includes(testStrFirst); const testStrSecond = 'org:customerservice:manage'; testArray.includes(testStrSecond); const testStrNotMatch = 'org:sales:write'; testArray.includes(testStrNotMatch);
String.includes
const testStrFirst = 'org:sales:manage'; testString.includes(testStrFirst); const testStrSecond = 'org:customerservice:manage'; testString.includes(testStrSecond); const testStrNotMatch = 'org:sales:write'; testString.includes(testStrNotMatch);
Array.includes sorted
const testStrFirst = 'org:sales:manage'; testArraySorted.includes(testStrFirst); const testStrSecond = 'org:customerservice:manage'; testArraySorted.includes(testStrSecond); const testStrNotMatch = 'org:sales:write'; testArraySorted.includes(testStrNotMatch);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
String.includes
Array.includes sorted
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0
Browser/OS:
Firefox 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
16226287.0 Ops/sec
String.includes
1205766784.0 Ops/sec
Array.includes sorted
16899906.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Overview** The benchmark compares the performance of three different ways to check if a string is included in an array: `Array.includes`, `String.includes` (which uses `Array.prototype.includes`), and `Array.includes sorted` (which sorts the array before performing the lookup). The goal is to determine which approach is the fastest. **Test Cases** The benchmark consists of three individual test cases: 1. **Array.includes**: This test case checks if two strings (`testStrFirst` and `testStrSecond`) are included in the `testArray`. It also checks if a third string (`testStrNotMatch`) is not included. 2. **String.includes**: This test case uses the `String.prototype.includes` method to check if the same two strings (`testStrFirst` and `testStrSecond`) are included in the same string (`testString`). It also checks if a third string (`testStrNotMatch`) is not included. 3. **Array.includes sorted**: This test case sorts the `testArray` before performing the lookup to check if the same two strings (`testStrFirst` and `testStrSecond`) are included in the sorted array. **Library Used** In all three test cases, the `includes` method is used, which is a part of the ECMAScript standard. However, the first two test cases use `String.prototype.includes`, which is a wrapper around `Array.prototype.includes`. The third test case uses `Array.prototype.includes` directly. **Special JS Feature** None of the test cases rely on any special JavaScript features or syntax beyond what is required for the `includes` method. **Pros and Cons of Each Approach** 1. **Array.includes**: This approach sorts the array before performing the lookup, which can lead to a performance improvement if the array is already sorted. * Pros: Can be faster when the array is already sorted. * Cons: Requires extra memory to store the sorted array and may incur additional overhead due to sorting. 2. **String.includes**: This approach uses `Array.prototype.includes` to check if the string contains a specific substring, which can lead to a performance improvement if the string is large. * Pros: Can be faster for large strings due to optimized implementation in WebKit-based browsers. * Cons: May not work as expected with non-ASCII characters or non-English strings. 3. **Array.includes sorted**: This approach sorts the array before performing the lookup, which can lead to a performance improvement if the array is already sorted. * Pros: Can be faster when the array is already sorted and the strings are comparable. * Cons: Requires extra memory to store the sorted array and may incur additional overhead due to sorting. **Other Alternatives** If you want to explore alternative approaches, here are a few options: 1. **Use `indexOf` instead of `includes`:** While `includes` is generally faster than `indexOf`, it's not always the case. In some cases, using `indexOf` might be slightly faster due to optimizations in WebKit-based browsers. 2. **Use `Map` instead of arrays:** If you're working with a large number of unique strings, using a `Map` data structure can provide faster lookup times than using an array. 3. **Use a custom implementation:** Depending on your specific use case and requirements, you might be able to create a custom implementation that provides better performance than the built-in `includes` method. Keep in mind that these alternatives may require more code and effort to implement, but they can provide significant performance improvements in certain scenarios.
Related benchmarks:
String.indexOf vs Array split and includes
slice sort vs spread sort vs sort vs structured sort
=== vs includes
equals vs includes
localeCompare compare
Comments
Confirm delete:
Do you really want to delete benchmark?