<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.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);
R.includes
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.includes | |
array.includes | |
ramda includes |
Test name | Executions per second |
---|---|
_.includes | 3359197.5 Ops/sec |
array.includes | 13848172.0 Ops/sec |
ramda includes | 7773584.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The provided JSON represents a JavaScript microbenchmark that tests the performance of three different approaches to check if an element exists in an array:
_.includes
(Ramda library): This function checks if an element is present in an array using Ramda's includes
function.array.includes
: This is a built-in JavaScript method that checks if an element is present in an array.R.includes
(Ramda library): This is another version of the includes function provided by Ramda, but with slightly different behavior.Options compared
The three options are being compared to measure their performance difference:
_.includes
and array.includes
: These built-in JavaScript methods are likely to be fast and efficient since they're implemented in native code.R.includes
: This Ramda function might have some overhead due to its use of a custom implementation, but it may also offer benefits like better type inference or more concise code._.includes
and array.includes
: These methods require the array to be in scope, which can add extra memory allocations or garbage collection pauses depending on the context.R.includes
: This Ramda function might not be as efficient as the built-in methods, but it could also lead to more predictable performance since it's implemented in a specific way.Library and its purpose
lodash.js
(Lodash library): Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like string manipulation, array processing, and more. The _.includes
function is part of the Lodash library and is used here as a benchmarking tool.ramda.min.js
(Ramda library): Ramda is another functional programming library for JavaScript that offers a more concise and expressive way to write code. The R.includes
function is an example of how Ramda can implement common array operations in its own way.Special JS feature or syntax
There doesn't seem to be any special JavaScript features or syntax being used here. All the code looks like standard ES6+ JavaScript.
Other alternatives
If you're interested in exploring alternative approaches, here are a few examples:
Array.prototype.some()
or Array.prototype.every()
: These methods can be used as an alternative to array.includes
since they also check for existence.Keep in mind that this benchmark focuses on performance, so using an alternative implementation like Array.prototype.some()
or a simple loop might skew the results due to extra overhead.