Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search and item
(version: 0)
Comparing performance of:
Array.some vs Array.filter vs Array.indexOf vs Array.includes vs Array.find
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var firstEl = {"id": 0} var listA = [firstEl] var listB = [firstEl] for (var i = 0; i < 10000; i++) { listA.push({"id":Math.floor(Math.random() * (i - 1) + 1)}); listB.push({"id":Math.floor(Math.random() * (i - 1) + 1)}); } var lastEl = {"id": 10001}
Tests:
Array.some
const difference1 = listA.some(e => e.id === firstEl.id) const difference2 = listA.some(e => e.id === lastEl.id)
Array.filter
const difference1 = listA.filter(e => e.id === firstEl.id) const difference2 = listA.filter(e => e.id === lastEl.id)
Array.indexOf
const difference1 = listA.indexOf(e => e.id === firstEl.id) const difference2 = listA.indexOf(e => e.id === lastEl.id)
Array.includes
const difference1 = listA.includes(e => e.id === firstEl.id) const difference2 = listA.includes(e => e.id === lastEl.id)
Array.find
const difference1 = listA.find(e => e.id === firstEl.id) const difference2 = listA.find(e => e.id === lastEl.id)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Array.some
Array.filter
Array.indexOf
Array.includes
Array.find
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 dive into the details of what's being tested in this benchmark. **Benchmark Description** The test case measures the performance of four different methods for finding an item with a specific `id` in an array: 1. `Array.some()` 2. `Array.filter()` 3. `Array.indexOf()` 4. `Array.includes()` Each method is called twice, once searching for an element that matches the `id` of the first element (`firstEl`) and once searching for an element that matches the `id` of the last element in the array (`lastEl`). **What's being tested?** The test is comparing the performance of each method: * How fast are they to find a single match? * Are there any differences in performance between finding an element with a specific `id` versus finding all elements with that `id`? **Options compared:** 1. **Array.some()**: Returns `true` if at least one element of the array satisfies the provided callback. 2. **Array.filter()**: Creates a new array with all elements that pass the test implemented by the provided function. 3. **Array.indexOf()**: Returns the index of the first element in the array that satisfies the provided callback. 4. **Array.includes()**: Returns `true` if at least one element of the array is equal to the specified value. **Pros and Cons:** 1. **Array.some():** * Pros: * Simple to implement * Can be faster than other methods for very large arrays * Cons: * Returns `true` as soon as it finds a match, which might not be what you want * Does not preserve the original array structure 2. **Array.filter():** * Pros: * Preserves the original array structure * Can be used to create a new array with all matching elements * Cons: * Creates a new array, which can consume more memory * Returns `undefined` if no matches are found 3. **Array.indexOf():** * Pros: * Fast and efficient * Returns the index of the first match, not just whether there's one * Cons: * Throws an error if the array is empty or the element is not found 4. **Array.includes():** * Pros: * Similar to `indexOf()`, but returns `false` instead of throwing an error * Faster than `filter()` for small arrays * Cons: * Returns `true` as soon as it finds a match, which might not be what you want **Library/Features:** None mentioned in the benchmark definition. **Special JS Features/Syntax:** None mentioned in the benchmark definition. **Other Alternatives:** For similar use cases, consider using other methods like: * `Array.every()` for finding all elements that satisfy a condition * `Array.map()` and `Array.reduce()` for transforming or aggregating arrays based on conditions Keep in mind that performance differences between these methods can vary depending on the specific use case and array size. The alternatives listed above may offer different trade-offs in terms of simplicity, memory usage, or speed.
Related benchmarks:
Array vs object literal
Array vs object literal v2
sorting
Search: Array to Map and find vs Array.find 2
Test forEach (findIndex) vs forEach (include)
Comments
Confirm delete:
Do you really want to delete benchmark?