Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Ramda's `where` vs. my `whereAll` - Part 2 - Example Code From the Documentation for `R.where`
(version: 5)
Comparing performance of:
Using `R.where` vs Using `whereAll`
Created:
9 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/ramda@0.23.0/dist/ramda.js"></script>
Script Preparation code:
// Sort of like a recursive version of Ramda's `where` (http://ramdajs.com/docs/#where). var whereAll = R.curry((spec, obj) => { if (typeof obj === "undefined") { if (spec === null || typeof spec === "boolean") { return !spec } return false } else if (spec === false) { return false } var output = true R.forEachObjIndexed((v, k) => { if (v === null || typeof v === "boolean" || R.keys(v).length) { if (!whereAll(v, obj[k])) { output = false } } else if (!v(obj[k])) { output = false } }, spec) return output }) // ----------------------------------------------------------------------------- var pred1 = R.where({ a: R.equals("foo"), b: R.complement(R.equals("bar")), x: R.gt(R.__, 10), y: R.lt(R.__, 20), }) var pred2 = whereAll({ a: R.equals("foo"), b: R.complement(R.equals("bar")), x: R.gt(R.__, 10), y: R.lt(R.__, 20), })
Tests:
Using `R.where`
pred1({a: "foo", b: "xxx", x: 11, y: 19}) pred1({a: "xxx", b: "xxx", x: 11, y: 19}) pred1({a: "foo", b: "bar", x: 11, y: 19}) pred1({a: "foo", b: "xxx", x: 10, y: 19}) pred1({a: "foo", b: "xxx", x: 11, y: 20})
Using `whereAll`
pred2({a: "foo", b: "xxx", x: 11, y: 19}) pred2({a: "xxx", b: "xxx", x: 11, y: 19}) pred2({a: "foo", b: "bar", x: 11, y: 19}) pred2({a: "foo", b: "xxx", x: 10, y: 19}) pred2({a: "foo", b: "xxx", x: 11, y: 20})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using `R.where`
Using `whereAll`
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Definition** The benchmark is comparing two approaches to implement the `where` function, which filters an object based on a set of conditions. In JavaScript, this can be achieved using Ramda's `R.where` or by creating our own implementation, denoted as `whereAll`. **Options Compared** We have two options being compared: 1. **Ramda's `R.where`**: This is a built-in function in the Ramda library, which provides a concise and expressive way to filter objects using functions. 2. **Custom implementation: `whereAll`**: This is our own implementation of the `where` function, written in JavaScript. **Pros and Cons** Here are some pros and cons for each option: **Ramda's `R.where`** Pros: * Concise and expressive code * Well-tested and maintained library * Easy to understand and use Cons: * External dependency on Ramda library * May not be suitable for all environments (e.g., Node.js, browser) **Custom implementation: `whereAll`** Pros: * No external dependencies * Can be optimized for specific use cases or environments * More control over the implementation Cons: * Requires more code and understanding of functional programming concepts * May be less efficient than Ramda's implementation due to additional overhead **Library Used (Ramda)** The `where` function in Ramda is a higher-order function that takes two arguments: an object to filter and a predicate function. The predicate function returns a boolean value indicating whether the condition is met for each property of the object. **Special JS Feature/ Syntax** There are no special JavaScript features or syntax used in this benchmark. However, it's worth noting that Ramda uses a functional programming style, which can be unfamiliar to some developers. **Other Considerations** When choosing between these two options, consider the following: * If you need a concise and expressive way to filter objects, Ramda's `R.where` might be a good choice. However, if you're working on a project with limited dependencies or specific optimization needs, the custom implementation (`whereAll`) might be more suitable. * Keep in mind that Ramda's implementation may have additional overhead due to its functional programming style. **Alternatives** If you're interested in exploring other alternatives, here are some options: * Lodash: Another popular JavaScript library with a `where` function similar to Ramda's. * Microsoft's Filter: A simpler filtering function provided by the JavaScript engine. * Vanilla JavaScript: You can implement your own filtering functions using basic JavaScript constructs like loops and conditional statements. I hope this explanation helps you understand what's being tested in this benchmark!
Related benchmarks:
Ramda's `where` vs. my `whereAll` - Part 1 - Single Property Tests
Ramda's `where` vs. my `whereAll` - Part 3 - Nested Spec
Ramda vs. VanillaJS - Find an object by prop
Ramda isNil vs vanilla JS
Comments
Confirm delete:
Do you really want to delete benchmark?