<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')
_.indexOf(array, 'sausage') !== 1
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
IndexOf | |
Includes | |
lodash |
Test name | Executions per second |
---|---|
IndexOf | 20586304.0 Ops/sec |
Includes | 21658948.0 Ops/sec |
lodash | 7097785.5 Ops/sec |
Let's break down the provided JSON and benchmarking code to understand what is being tested.
Overview
The MeasureThat.net
website allows users to create and run JavaScript microbenchmarks. The provided benchmark compares three approaches for searching an array of strings: indexOf
, includes
, and a custom implementation using the lodash
library.
Script Preparation Code
The script preparation code creates an array array
with three elements: 'banana'
, 'sausage'
, and 'jesus'
.
var array = ['banana', 'sausage', 'jesus'];
Html Preparation Code
The HTML preparation code includes a reference to the lodash.js
library, which is used in one of the test cases.
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Individual Test Cases
There are three test cases:
indexOf('sausage')
returns 1.includes('sausage')
returns a boolean value (true or false)._.indexOf(array, 'sausage') !== 1
using the lodash
library.What is tested
The benchmark tests the performance of each approach:
indexOf
: The native JavaScript method for finding the index of an element in an array.includes
: A built-in JavaScript method that checks if an array contains a specified value._
.indexOf: A method from the lodash
library that performs the same operation as indexOf
.Options compared
The benchmark compares two approaches:
indexOf
and includes
)lodash
library's _.indexOf
methodPros and Cons of each approach
lodash
library) and may have a slight performance overhead due to the added function call.Special considerations