Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some-2
(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 array = [ "inc", "co", "corp", "corps", "corporation", "corporate", "company", "service", "services", "center", "llc", "of", "svc", "svcs", "grp", "grps", "group", "groups", "systems", "sys", "and", "intl", "international", "national", "construction", "the", "city", "county", "trucking", "enterprises", "enterprise", "ctr", "products", "american", "america", "technology", "technologies", "ltd", "limited", "univ", "university", "fndtn", "foundation", "us", "usa", "express", "for", "bank", "valley", "school", "medical", "pharmacy", "associates", "health", "md", "pc", "care", "clinic", "hospital", "family", "pa", "medicine", "physicians", "healthcare", "home", "drug", "eye", "at", "community", "assoc", "st", "practice", "cvs", "general", "dr", "doctor", "inactive" ]
Tests:
IndexOf
array.indexOf('Ltd') !== 1
Includes
array.includes('Ltd')
some
array.some(v => v === 'Ltd')
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 what's being tested on the provided JSON. **Benchmark Purpose:** The benchmark measures the performance of three different methods to check if an array contains a specific value: 1. `array.indexOf()` 2. `array.includes()` 3. `array.some()` with a callback function that checks for equality **Comparison Options:** 1. **`array.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. **`array.includes()`**: This method returns a boolean indicating whether the array includes the specified value. 3. **`array.some()` with equality check**: This method returns a boolean indicating whether at least one element in the array satisfies the provided callback function (in this case, `v => v === 'Ltd'`). **Pros and Cons:** * **`array.indexOf()`**: Pros: simple, fast for finding a specific value. Cons: returns an index, not a boolean; may return -1 if not found, which can be falsey in some contexts. * **`array.includes()`**: Pros: straightforward, easy to read. Cons: slower than `indexOf()` for large arrays; may be less efficient due to the extra function call. * **`array.some()` with equality check**: Pros: concise and expressive. Cons: slower than `indexOf()` or `includes()` for finding a single value; returns a boolean result. **Library Usage:** The benchmark uses the Lodash library, which provides the `includes()` method. The inclusion of this library is specified in the HTML Preparation Code section of the benchmark definition. **Special JS Features/Syntax:** None mentioned explicitly in the provided benchmark definition. However, note that `some()` is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Other Considerations:** * The benchmark measures performance across different browsers and devices, which can affect results due to differences in engine optimizations or caching. * The array used for testing contains a large number of elements with similar values, which may impact the performance comparison. **Alternatives:** For finding a specific value in an array: 1. `array.indexOf()` (most efficient) 2. `includes()` (slightly slower than `indexOf()`) 3. `some()` with equality check (slower for finding a single value) For iterating over an array and performing some action on each element, consider using `forEach()` or `map()`.
Related benchmarks:
indexOf vs includes vs some - 20211114
array indexOf vs includes vs some1
array indexOf (gt -1) vs includes vs some
arrays comparison indexOf vs includes vs some
Comments
Confirm delete:
Do you really want to delete benchmark?