<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
var primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,97]
_.includes(primes, 47)
primes.includes(79)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.includes | |
array.includes |
Test name | Executions per second |
---|---|
_.includes | 5948375.5 Ops/sec |
array.includes | 11205820.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested, compared, and the pros/cons of each approach.
Benchmark Definition
The benchmark defines two test cases:
_.includes(primes, 47)
: This is a microbenchmark that tests the performance of the Lodash library's _.includes
function.primes.includes(79)
: This is another test case that compares the performance of the built-in array method includes()
with the _.includes
function from Lodash.Script Preparation Code
The script preparation code defines a JavaScript array primes
and assigns it to a variable:
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 97];
This array is used in both test cases.
Html Preparation Code
The HTML preparation code includes a link to load the Lodash library:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
This library provides the _.includes
function used in one of the test cases.
Options Compared
The two test cases compare two different approaches to check if an element exists in an array:
Pros and Cons
Here are some pros and cons of each approach:
Special JS Features/Syntax
There are no special JavaScript features or syntax used in this benchmark. The focus is on comparing the performance of two different approaches to check if an element exists in an array.
Other Alternatives
If you want to test other approaches, you could consider using:
lodash-es
(a smaller and more modern version of Lodash) or ramda
.indexOf()
, includes()
, or some()
with a custom callback function.Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preferences.