Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes2
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs lodash
Created:
4 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 = ['banana','banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'sausage', 'jesus']
Tests:
IndexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
lodash
_.includes(array, 'sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IndexOf
Includes
lodash
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 world of JavaScript microbenchmarks! **What is tested on the provided JSON?** The benchmark measures the performance of three different approaches to check if an element exists in an array: `indexOf`, `includes`, and `_includes` (using the Lodash library). **Options compared** Here are the options being compared: 1. **`indexOf`**: This method searches for the specified value in the array, returning its first occurrence or -1 if it's not found. 2. **`includes`**: This method checks if an element exists in an array, returning a boolean value indicating whether it does or not. 3. **`_.includes` (Lodash)**: This is a higher-order function that wraps the `includes` method, providing additional functionality and options. **Pros and Cons of each approach** 1. **`indexOf`**: * Pros: + Returns the index of the first occurrence if found. + Can be used to get the index of the first element in an array with a specific value. * Cons: + Returns -1 if the element is not found, which can lead to unnecessary computations. + May perform slower than other methods due to its search algorithm. 2. **`includes`**: * Pros: + Faster than `indexOf`, as it only checks for existence and doesn't return an index. + Returns a boolean value (true/false), making it easier to handle results in code. * Cons: + Does not return the index of the first occurrence if found. 3. **`_.includes` (Lodash)**: * Pros: + Provides additional functionality and options, such as customization for edge cases. + Can be more concise than using `indexOf` or `includes`. * Cons: + Requires an external library (Lodash), which may add unnecessary overhead. **Library usage** The Lodash library is used to implement the `_includes` function. Lodash is a popular utility library for JavaScript that provides various functions and methods to simplify common tasks, such as array manipulation, string formatting, and more. **Special JS feature/syntax** This benchmark does not use any special JavaScript features or syntax that would require a deep understanding of advanced concepts like ES6+ features or experimental APIs. The focus is on the performance comparison between three simple, widely used methods. **Other alternatives** If you're interested in exploring alternative approaches, here are some additional options: 1. **`find()`**: This method returns the first element that satisfies a provided condition. 2. **`some()`**: This method returns true if at least one element of the array satisfies a provided condition. 3. **Custom implementations**: You can also write your own custom implementation for checking existence in an array, using techniques like binary search or iterating through the array manually. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the methods being tested here.
Related benchmarks:
IndexOf vs Includes vs lodash includes
IndexOf vs Includes vs lodash includes test2
IndexOf vs Includes vs lodash includes for string
array IndexOf vs array Includes vs lodash indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?