Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test includes versus indexof
(version: 0)
Test
Comparing performance of:
includes vs indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['1', 2, '3'];
Tests:
includes
arr.includes('3');
indexOf
arr.indexOf('3') >= 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
indexOf
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is comparing two approaches to check if an element exists in an array: 1. `arr.includes('3')` 2. `arr.indexOf('3') >= 0` **Script Preparation Code** ```javascript var arr = ['1', 2, '3']; ``` This code creates a sample array `arr` containing three elements. **Options Compared** The two options being compared are: 1. **includes()**: This method returns `true` if the specified element is found in the array, and `false` otherwise. 2. **indexOf() >= 0**: This approach checks if the index of the element in the array is greater than or equal to 0. If it is, the element exists in the array. **Pros and Cons** 1. **includes()** * Pros: + More concise and readable code. + Often preferred by developers due to its simplicity and readability. * Cons: + Can be slower for large arrays since it iterates through the entire array to find a match. 2. **indexOf() >= 0** * Pros: + Can be faster than `includes()` for large arrays, as it stops iterating once it finds the first match or reaches the end of the array. * Cons: + Less readable and more verbose code. **Library Used** None in this benchmark. Both options are built-in JavaScript methods. **Special JS Feature/Syntax** None mentioned in this benchmark. **Other Alternatives** For checking if an element exists in an array, you can also use: 1. `in`: This operator returns a boolean value indicating whether the specified property (or key) is present in the object. 2. `find()`, `findIndex()`: These methods return the first element that matches the provided function or predicate. **Benchmark Preparation Code** The code preparation section provides the initial setup for the benchmark, creating a sample array `arr` and making it available to both test cases. **Individual Test Cases** Two test cases are defined: 1. **includes** 2. **indexOf** These test cases measure the performance of each approach individually. **Latest Benchmark Result** The provided result shows the execution count per second for each test case, indicating how many times each operation was executed in a given time frame. In summary, this benchmark compares two approaches to check if an element exists in an array: `includes()` and `indexOf() >= 0`. While both methods have their pros and cons, `includes()` is often preferred due to its simplicity but may be slower for large arrays. The benchmark provides valuable insights into the performance of these operations on various devices and browsers.
Related benchmarks:
bitwise vs compare vs includes
JavaScript Benchmark: includes vs indexOf
IndexOf > vs Includes
JS indexOf vs some
Comments
Confirm delete:
Do you really want to delete benchmark?