<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = 'bananasausagejesus'
array.indexOf('sausage') !== -1
array.includes('sausage')
_.includes(array, 'sausage')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
IndexOf | |
Includes | |
lodash |
Test name | Executions per second |
---|---|
IndexOf | 72100096.0 Ops/sec |
Includes | 76355080.0 Ops/sec |
lodash | 49683728.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares three methods for checking if a string exists within an array:
array.indexOf('sausage') !== -1
array.includes('sausage')
_.includes(array, 'sausage')
(using the Lodash library)Options Being Compared
Here's what each option is doing:
indexOf
method'sausage'
) in the array. If the value is not found, it returns -1
.includes
method'sausage'
) exists in the array.indexOf
, and can be faster for large arrays since it uses a optimized implementation._.includes
method (using Lodash)'sausage'
) exists in the array.Library Used
The _.includes
method is using the Lodash library, which provides a comprehensive set of utility functions for JavaScript. Lodash is widely used in web development and is known for its performance and reliability.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on comparing three well-established methods for string existence checks.
Other Alternatives
If you're interested in exploring alternative approaches, here are a few options:
: This approach combines the
indexOfmethod on strings with the
includes` method on arrays. While it may seem like an obvious combination, it's not always the most efficient or readable solution.Overall, the benchmark is providing valuable insights into the performance characteristics of different string existence checks in modern web browsers. By comparing three well-established methods, it's helping users understand the trade-offs between readability, maintainability, and raw performance.