Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arrays comparison indexOf vs includes vs some
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
IndexOf vs Includes vs some
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array1 = [ "Toyota", "Honda", "Ford", "Chevrolet", "Nissan", "Volkswagen", "BMW", "Mercedes-Benz", "Audi", "Hyundai", "Jeep", "Kia", "Subaru", "Mazda", "Lexus", "Volvo", "Tesla", "Cadillac", "Acura", "Porsche", "Infiniti", "Buick", "GMC", "Jaguar", "Chrysler" ]; var array2 = ["Audi", "Volvo", "Porsche", "Tesla", "Hyundai", "Mazda", "Honda", "Nissan", "Jeep", "Toyota"];
Tests:
IndexOf
array1.filter((n) => array2.indexOf(n) !== -1);
Includes
array1.filter((n) => array2.includes(n));
some
array1.filter((n) => array2.some(el => el === n));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IndexOf
Includes
some
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 is being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three different approaches to check if an array contains a specific value: 1. `array1.filter((n) => array2.indexOf(n) !== -1)` 2. `array1.filter((n) => array2.includes(n))` 3. `array1.filter((n) => array2.some(el => el === n))` **Approaches Compared** Each approach uses a different method to check if an element is present in the `array2` array: 1. **IndexOf**: This method returns the index of the first occurrence of the specified value in the array, or -1 if it's not found. 2. **Includes**: This method returns a boolean indicating whether the array includes the specified value. 3. **Some**: This method returns a boolean indicating whether at least one element in the array satisfies the condition. **Pros and Cons** Here are some pros and cons of each approach: * **IndexOf**: + Pros: Fast and efficient, as it uses a linear search algorithm that stops as soon as it finds the match. + Cons: Returns the index of the first occurrence, which might not be what you want. Also, can be slow for large arrays if the value is not found at the beginning. * **Includes**: + Pros: Simple and easy to read, as it uses a simple boolean check. + Cons: Can be slower than `IndexOf` for large arrays, as it needs to iterate over all elements to confirm the presence of the value. * **Some**: + Pros: Efficient and fast, as it only iterates over one element at a time until it finds a match or reaches the end of the array. + Cons: Returns a boolean value, which might require additional checks in your code. **Library Used** The benchmark uses Lodash.js, a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. In this case, it's used to implement the `some` method. **Special JS Features or Syntax** None are mentioned in the provided code. **Other Alternatives** If you're looking for alternative approaches, here are some options: 1. **Array.prototype.includes()**: This is a modern JavaScript method that provides a similar functionality to the `Includes` approach. 2. **Array.prototype.indexOf()**: Similar to `IndexOf`, but returns -1 if not found instead of -1 as a special value. 3. **Set data structure**: If you need to perform frequent lookups, using a Set data structure might be more efficient than arrays or objects. In summary, the benchmark provides a good starting point for understanding the performance differences between these three approaches. The choice of approach depends on your specific use case and requirements.
Related benchmarks:
IndexOf vs Includes
array indexOf vs includes vs some using numbers
Array .indexOf vs .some vs .includes
array indexOf (gt -1) vs includes vs some
my own array: indexOf vs includes vs some
Comments
Confirm delete:
Do you really want to delete benchmark?