Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array indexOf vs includes vs some1
(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):
I'll provide an explanation of the provided JSON, highlighting what's being tested and the pros/cons of each approach. **Benchmark Definition** The benchmark is designed to compare the performance of three different methods for searching if an array contains a specific value: 1. `array.indexOf(value) !== 1`: This method uses `indexOf` to find the index of the first occurrence of the specified value. If the value is not found, it returns -1. 2. `array.includes(value)`: This method uses the `includes` method to check if the array contains the specified value. 3. `array.some(v => v === value)`: This method uses a callback function with the `some` method to iterate through the array and find the first occurrence of the specified value. **Options Compared** The benchmark is comparing three different approaches: 1. **IndexOf**: The original way to search for an element in an array, which returns the index of the first occurrence if found. 2. **Includes**: A more modern approach introduced in ECMAScript 2019, which provides a simple and efficient way to check if an array contains a specific value. 3. **Some**: An older approach that uses a callback function with `some` to iterate through the array and find the first occurrence of the specified value. **Pros/Cons** Here's a brief summary of each option: 1. **IndexOf**: * Pros: Wide support, easy to understand, and has been around for a long time. * Cons: Can be slower than `includes` due to its iteration process. 2. **Includes**: * Pros: Faster than `indexOf` since it uses a built-in method, more efficient, and less memory-intensive. * Cons: Introduced relatively recently (ECMAScript 2019), may not support older browsers or environments. 3. **Some**: * Pros: Can be useful when you need to perform additional checks on the elements being compared. * Cons: Requires a callback function, which can make it harder to read and maintain. **Library Usage** The benchmark uses the `lodash.js` library, specifically the `includes` method. This is likely due to the fact that `lodash.includes` provides a more efficient and reliable implementation of the `includes` method compared to the built-in JavaScript version. **Special JS Features/ Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you're looking for alternative approaches, here are a few: 1. **Array.prototype.findIndex**: Similar to `indexOf`, but with a more concise syntax. 2. **Array.prototype.includes**: As mentioned earlier, this is the modern approach introduced in ECMAScript 2019. 3. **Filter** and **some**: You can use the `filter` method to create an array of elements that match the condition, and then use `some` to check if any elements are present. Keep in mind that the choice of approach depends on your specific use case and requirements.
Related benchmarks:
indexOf vs includes vs some - 20211114
array indexOf vs includes vs some-2
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?