Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance of js Include function over array and string
(version: 0)
Comparing performance of:
test includes on array vs test includes on string
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
test includes on array
const b = ['Draft', 'Launcher' , 'InQueue', 'PartlySettled'].includes('InQueue');
test includes on string
const b = 'Draft|Launcher|InQueue|PartlySettled'.includes('InQueue');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
test includes on array
test includes on string
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
test includes on array
141620176.0 Ops/sec
test includes on string
223389936.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is testing the performance of the `includes()` function in JavaScript, specifically comparing its performance when used with arrays versus strings. **Script Preparation Code** There is no script preparation code provided, which means that the script to be tested will be generated automatically by MeasureThat.net based on the benchmark definition. **Html Preparation Code** Similarly, there is no html preparation code provided, indicating that the HTML to be executed alongside the JavaScript script will also be generated automatically. **Individual Test Cases** There are two test cases: 1. **test includes on array**: This test case defines a constant `b` using an array and checks if the element `'InQueue'` is present in it. ```javascript const b = ['Draft', 'Launcher' , 'InQueue', 'PartlySettled'].includes('InQueue'); ``` 2. **test includes on string**: This test case defines a constant `b` using a string with pipes (`|`) separating the values and checks if the element `'InQueue'` is present in it. ```javascript const b = 'Draft|Launcher|InQueue|PartlySettled'.includes('InQueue'); ``` **Pros and Cons of Different Approaches** The two approaches tested here are: * **Array-based `includes()`**: This approach uses the `includes()` function with an array as its argument. * **String-based `includes()` with pipes**: This approach uses the `includes()` function with a string containing pipes separating the values. **Pros and Cons:** * **Array-based `includes()`:** + Pros: - More efficient for large arrays, since `includes()` only scans the array until it finds the match. - Can be faster for certain use cases, such as when searching for an element in a sorted array. + Cons: - May not perform well if the array is very large or contains many duplicates, since `includes()` still needs to scan the entire array. * **String-based `includes()` with pipes:** + Pros: - More efficient than using the `split()` function to create an array of values and then use `includes()`. + Cons: - Requires a string that is specially formatted, which may be less readable or maintainable for some developers. - Can still perform poorly if the string is very large or contains many duplicates. **Library** None of the test cases uses any external libraries. The JavaScript built-in `includes()` function is used in both test cases. **Special JS Features or Syntax** None of the test cases use any special JavaScript features or syntax that are not commonly supported by most browsers. **Other Alternatives** If you want to explore alternative approaches, here are a few options: * **Using `findIndex()` instead of `includes()`:** This can be faster for certain use cases, since it returns the index of the first match, rather than just whether there is a match. * **Using regular expressions:** Regular expressions can provide more flexibility and power when searching for patterns in strings, but may also introduce additional overhead. In conclusion, the benchmark provided by MeasureThat.net is testing the performance of two different approaches to using the `includes()` function in JavaScript: one with arrays and one with strings. The results will help developers understand which approach is faster and more efficient for their specific use cases.
Related benchmarks:
equality vs includes
chain of or equals vs includes but smaller
=== vs includes
equals vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?