<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = ['banana', 'sausage', 'jesus']
array.indexOf('sausage') !== 1
array.includes('sausage')
_.includes(array, 'sausage')
_.indexOf(array, 'sausage') > 1
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
IndexOf | |
Includes | |
lodash includes | |
Lodash indexOf |
Test name | Executions per second |
---|---|
IndexOf | 12658318.0 Ops/sec |
Includes | 13369631.0 Ops/sec |
lodash includes | 3983201.8 Ops/sec |
Lodash indexOf | 4431262.5 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Description
The provided JSON represents a JavaScript microbenchmark that tests the performance of different approaches for checking if an element exists in an array: indexOf
, includes
, and two variations of lodash
functions (_.includes
and _.indexOf
). The goal is to determine which approach is the fastest.
Options Compared
The benchmark compares four options:
indexOf
: This function checks if a specific element exists in an array by searching from the start of the array.includes
: This function checks if a value exists in an array by searching for the exact match._.includes
function, which is part of the Lodash library. It provides additional features and optimizations compared to the native includes
function._.indexOf
function, which is part of the Lodash library.Pros and Cons
Here are some pros and cons for each approach:
indexOf
:includes
:indexOf
for non-exact matches, and its performance can degrade with very large arrays._lodash includes_
, but optimized for indexOf
specifically.Library Overview
The Lodash library is a popular JavaScript utility library that provides a comprehensive set of functional programming helpers, including string manipulation, array operations, and more. In this benchmark, Lodash is used to implement two optimized versions of indexOf
and includes
.
Special JS Feature or Syntax
None of the test cases use any special JavaScript features or syntax.
Alternative Approaches
Other approaches that could be considered for performance testing include:
indexOf
and includes
.Keep in mind that the specific alternatives will depend on the goals and scope of the benchmarking project.