<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var result = _.includes(array, 'd');
var result = array.includes('d');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash includes method | |
es6 includes method |
Test name | Executions per second |
---|---|
lodash includes method | 8203975.5 Ops/sec |
es6 includes method | 30497984.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided benchmark compares the performance of two approaches for checking if an element is included in an array: the includes
method (introduced in ECMAScript 2015) and the Lodash library's includes
function.
Options Compared
Two options are compared:
includes
keyword to check if a value is present in an array.Pros and Cons
Pros:
includes
method is a built-in function in modern JavaScript engines, making it efficient and well-optimized.includes
keyword is easy to read and write.Cons:
includes
method was introduced in ECMAScript 2015, older browsers may not support it. In this benchmark, both tests are run on Chrome 121, which supports ES6 features.includes
method, some older engines might have slower performance.Pros:
includes
method in older browsers due to its optimization and polyfilling efforts.Cons:
includes
function is a valid method, it's not as concise as the native includes
keyword.Library and Purpose
In this benchmark, Lodash is used to provide its includes
function, which takes two arguments: the array to search in and the value to search for. The purpose of this function is to check if the value is present in the array.
Special JS Feature or Syntax
None mentioned in the provided information.
Other Alternatives
If you're interested in exploring other options, here are a few alternatives:
includes
, other native JavaScript methods for working with arrays include indexOf()
, lastIndexOf()
, and some()
.Keep in mind that the choice of implementation depends on your specific use case, project requirements, and performance considerations.