<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
var arr = ['a', 'b', 'c'];
R.contains('b', arr)
arr.indexOf('b')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ramdajs contains | |
vanilla |
Test name | Executions per second |
---|---|
ramdajs contains | 2931398.2 Ops/sec |
vanilla | 9992847.0 Ops/sec |
This benchmark compares the performance of two methods for checking if an element exists within an array:
R.contains('b', arr)
: This uses the contains
function from the Ramda library, a functional programming library for JavaScript.
contains
function is likely optimized for performance within the library's ecosystem.arr.indexOf('b')
: This uses the built-in JavaScript method indexOf
.
Other Considerations:
indexOf
is part of the imperative tradition in JavaScript. The benchmark highlights how different paradigms can influence performance. Alternatives:
find()
: This built-in method finds the first element that satisfies a condition in an array. It could be used if you need to find more than just the presence of an element.contains
.Let me know if you have any more questions about this benchmark!