<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
var arr = ['I','a','m','a','r','r','a','y','t','o','c','h','e','c','k'];
var result = arr.includes('y')
var result = R.includes('y', arr);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native | |
ramda |
Test name | Executions per second |
---|---|
native | 23415200.0 Ops/sec |
ramda | 7023228.5 Ops/sec |
Let's dive into the explanation.
The provided benchmark measures the performance difference between using native JavaScript includes()
method and Ramda's R.includes()
function to check if an element exists in an array.
Native JavaScript includes() method
The native JavaScript includes()
method is a built-in function that checks if a specified value (in this case, 'y') exists in the given array (arr
). This method returns true
if the value is found, and false
otherwise. The pros of using native includes()
are:
However, there are some cons to consider:
includes()
only checks for exact value matches; it doesn't provide additional features like finding the index of the element.Ramda's R.includes() function
The Ramda library provides a higher-order function called R.includes()
that takes two arguments: the value to search for ('y'
) and the array to search in (arr
). This function returns true
if the value is found, and false
otherwise. The pros of using Ramda's R.includes()
are:
R.includes()
can be used with other functions to create more complex queries.However, there are some cons to consider:
Other alternatives
In addition to native includes()
and Ramda's R.includes()
, other alternatives could be considered:
includes()
that can be used in place of the native implementation.In summary, when choosing between native JavaScript includes()
and Ramda's R.includes()
, consider factors such as performance, browser compatibility, and additional features required.