Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes() v.s || (ver.2 on multiple cases)
(version: 2)
"Array includes" v.s "||" To find out is a variable is equal to any of possible cases. Array.includes() is faster and easier to extend cases.
Comparing performance of:
Array.includes() vs ||
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = '123' var b = '456' var c = '789' var d = '999' var e = '999'
Tests:
Array.includes()
[a,b,c,d].includes(e)
||
a===e||b===e||c===e||d===e
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.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, compared, and what are the pros and cons of different approaches. **Benchmark Definition** The test aims to compare two methods for determining whether a variable is equal to any of possible cases: 1. **Array.includes()**: This method checks if a value exists in an array. 2. **|| (ver.2 on multiple cases)**: This is a shorthand way of writing multiple `===` comparisons. **Script Preparation Code** The script sets up five variables: `a`, `b`, `c`, `d`, and `e`. These values are used to create an array `[a, b, c, d]` and check if the variable `e` is included in this array using both methods. **Html Preparation Code** There is no HTML preparation code provided, which means that only JavaScript is being executed for this benchmark. **Individual Test Cases** The test consists of two individual cases: 1. **Array.includes()**: This case checks if `e` (which equals `'999'`) is included in the array `[a, b, c, d]` using `Array.includes(e)`. 2. **||**: This case checks if `e` is equal to any of the values `a`, `b`, `c`, or `d` using a series of `===` comparisons: `a === e || b === e || c === e || d === e`. **Pros and Cons** **Array.includes()**: Pros: * More concise and readable code * Easier to extend cases (e.g., add more elements to the array) * Built-in function, so it's likely to be optimized for performance Cons: * May not be as fast as a simple `===` comparison in some cases * Requires an array to search through **|| (ver.2 on multiple cases)**: Pros: * Can be faster than `Array.includes()` for small arrays or specific use cases * More control over the comparisons performed Cons: * Less readable and more verbose code * Harder to extend cases (e.g., add more elements to the array) * Requires manual handling of edge cases (e.g., null or undefined values) **Library: None** There is no library being used in this benchmark. **Special JS feature or syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** If you wanted to implement a custom version of the `||` method, you could use a loop that iterates over each element in the array and checks if it's equal to `e`. Here's an example: ```javascript function custom_or(arr, val) { for (let i = 0; i < arr.length; i++) { if (arr[i] === val) return true; } return false; } // Usage console.log(custom_or([a, b, c, d], e)); // Output: true or false ``` Note that this implementation is less efficient and more verbose than the original `||` method. The benchmark on MeasureThat.net provides valuable insights into the performance differences between these two methods. By running multiple executions per second, the test gives a realistic representation of how these methods would perform in real-world scenarios.
Related benchmarks:
set.has vs. array.includes (string input)
equality vs includes
set.has vs. array.includes (find 999,999 in 1,000,000)
=== vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?