Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique names include vs indexOf
(version: 0)
Comparing performance of:
includes vs indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array1 = ["Paul", "Marten", "Sue", "Jonas", "Lisa", "Charlotte", "Yve", "Frank", "Lee"]; var array2 = ["Sabine", "Leon", "Kimberly", "Johannes", "Frank", "Sue"];
Tests:
includes
const array3 = array2.filter(item => !array1.includes(item));
indexOf
const array3 = array2.filter((item) => array1.indexOf(item) === -1);
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 what's being tested in the provided JSON, and I'll explain the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is comparing two approaches to filter an array: 1. Using `includes()`: `array2.filter(item => !array1.includes(item))` 2. Using `indexOf()` with a negative value check: `const array3 = array2.filter((item) => array1.indexOf(item) === -1);` **Options Compared** The two options being compared are: * **Using `includes()`**: This method checks if an element is present in the array using the `includes()` function. It returns a boolean value (`true` or `false`) indicating whether the element is found. * **Using `indexOf()` with a negative value check**: This approach uses the `indexOf()` method to find the index of the first occurrence of an element in the array. If the element is not found, `indexOf()` returns `-1`. The test checks if the result of `indexOf()` is equal to `-1` to determine if the element is not present. **Pros and Cons** * **Using `includes()`**: + Pros: Concise, readable, and often faster than `indexOf()`. + Cons: May be slower for large arrays due to the overhead of the `includes()` function. * **Using `indexOf()` with a negative value check**: + Pros: Can be faster for large arrays, as it uses an optimized algorithm under the hood. + Cons: More complex and harder to read than the `includes()` approach. **Library** There is no library being used in this benchmark. However, it's worth noting that the `includes()` function is implemented using the ECMAScript 2015 (ES6) standard, which provides a more concise and readable way of checking for array presence. **Special JS Feature or Syntax** The use of arrow functions (`(item) => ...`) in the benchmark definition is a modern JavaScript feature introduced in ES6. It allows for concise and readable function definitions using a shorter syntax. **Other Considerations** * The benchmark uses a relatively small input array (`array2` with 8 elements), which may not accurately represent real-world performance scenarios. * The benchmark doesn't account for edge cases, such as empty arrays or arrays with duplicate elements. * The `ExecutionsPerSecond` metric is used to measure the execution time of each test case. This can be affected by various factors, including system load, hardware specifications, and browser optimizations. **Alternatives** Other alternatives for filtering arrays in JavaScript include: * Using a `forEach()` loop to iterate over the array and check for presence. * Using a `for...in` loop to access array elements directly. * Using a library like Lodash or Ramda, which provide more extensive array manipulation functions. However, it's worth noting that these alternatives may not be as concise or readable as the `includes()` function, and their performance may vary depending on the specific use case.
Related benchmarks:
IndexOf vs Includes
Array.includes() vs Array.indexOf()
IndexOf vs Includes array of numbers
Includes vs. IndexOf
Comments
Confirm delete:
Do you really want to delete benchmark?