Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Ramda's `where` vs. my `whereAll` - Part 3 - Nested Spec
(version: 14)
Comparing performance of:
Using `R.where` vs Using `whereAll` vs Using `whereAll` (alternate)
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 data1 = { a: { h: [ { i: 5 }, [ { j: 6, k: 7 }, { j: 8, k: "nine" }, ], 10, { l: { m: { n: false } } }, ], }, } var data2 = { a: { h: [ { i: 5 }, [ { j: 8, k: "nine" }, ], ], }, } var detect1 = R.where({ a: R.where({ h: R.where([ R.always(true), Array.isArray, R.complement(R.identical(undefined)), R.where({ l: R.where({ m: R.where({ n: R.complement(R.identical(undefined)) }) }) }), ]), }), }) var detect2 = whereAll({ a: { h: [ R.always(true), Array.isArray, R.complement(R.identical(undefined)), { l: { m: { n: R.complement(R.identical(undefined)) } } }, ], }, }) var detect3 = whereAll({ a: { h: [ null, Array.isArray, true, { l: { m: { n: true } } }, ], }, })
Tests:
Using `R.where`
detect1(data1) detect1(data2)
Using `whereAll`
detect2(data1) detect2(data2)
Using `whereAll` (alternate)
detect3(data1) detect3(data2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using `R.where`
Using `whereAll`
Using `whereAll` (alternate)
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):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance difference between Ramda's `where` function and a custom implementation called `whereAll`. We will break down what is being tested, compare different approaches, discuss pros and cons, and explore alternative libraries. **Benchmark Definition** The benchmark definition is a JSON object that contains the script preparation code, HTML preparation code, individual test cases, and latest benchmark results. The script preparation code defines two custom functions: `whereAll` and `R.where`, which are implementations of Ramda's `where` function. The `whereAll` function checks if all properties in an object match a certain condition. **Individual Test Cases** There are three test cases: 1. **Using `R.where`**: This test case uses Ramda's `where` function with the same conditions as the `whereAll` function. 2. **Using `whereAll`**: This test case uses the custom `whereAll` function defined in the script preparation code. 3. **Using `whereAll` (alternate)**: This test case uses another implementation of `whereAll`, which is different from the first one. **What is being tested?** The benchmark tests the performance difference between three implementations: * Ramda's `where` function * Two custom implementations of `whereAll` **Options Compared** The options compared are: 1. **Ramda's `where` function**: A built-in implementation of the `where` function from the Ramda library. 2. **Custom implementation 1 (whereAll)**: The first custom implementation of `whereAll`, defined in the script preparation code. 3. **Custom implementation 2 (whereAll alternate)**: Another custom implementation of `whereAll`, which is different from the first one. **Pros and Cons** Here are some pros and cons for each option: 1. **Ramda's `where` function** * Pros: + Built-in, optimized implementation + Easy to use and understand * Cons: + Might be slower than custom implementations due to overhead of the Ramda library 2. **Custom implementation 1 (whereAll)** * Pros: + Optimized for specific use cases (nested objects with arrays) + Customizable to fit specific requirements * Cons: + Requires manual handling of edge cases and potential issues with array manipulation 3. **Custom implementation 2 (whereAll alternate)** * Pros: + Might be faster than the first custom implementation due to different optimizations * Cons: + Unknown performance characteristics, as it's a different implementation **Library: Ramda** Ramda is a popular JavaScript library for functional programming. The `where` function in this benchmark is part of Ramda's API and provides a concise way to filter objects based on conditions. **Special JS Features or Syntax (None)** There are no special JavaScript features or syntax used in this benchmark, except for the use of Ramda's `curry` method to create higher-order functions. **Other Alternatives** If you need to implement a similar filtering mechanism, consider using: * Lodash's `filterBy` function * A simple recursive function implementation * A library like Immutable.js or MobX for state management and filtering In conclusion, the benchmark measures the performance difference between Ramda's `where` function and two custom implementations of `whereAll`. The choice of implementation depends on specific requirements, optimization needs, and familiarity with functional programming.
Related benchmarks:
Ramda's `where` vs. my `whereAll` - Part 1 - Single Property Tests
Ramda's `where` vs. my `whereAll` - Part 2 - Example Code From the Documentation for `R.where`
ramda-transducer w/mutations v2
Ramda isNil vs vanilla JS
Comments
Confirm delete:
Do you really want to delete benchmark?