Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.map.filter vs transducers v2
(version: 0)
Comparing performance of:
xform vs classic vs classic v2 vs xform2
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.min.js'></script> <script> const data = [...Array(10000).keys()].map(() => ({ val: Math.floor(Math.random() * 10000) })) const isEven = i => i % 2 === 0 // Transducers const mapping = f => reducing => (result, input) => reducing(result, f(input)) const filtering = pred => reducing => (result, input) => pred(input) ? reducing(result, input) : result const uniq = () => reducing => { let pv return (result, input) => { const prior = pv pv = input return prior === input ? result : reducing(result, input) } } const xform = R.compose( mapping(x => x.val), mapping(x => x + 2), uniq(), filtering(isEven) ); const transduce = (xform, f, init, coll) => coll.reduce(xform(f), init) const conj = (a, b) => !!b ? [...a, b] : a const conjm = (a, b) => !!b ? (a.push(b) && a) : a const print = (label, res) => console.log(label, res.reduce((a, b) => a + b, 0)) </script>
Tests:
xform
transduce(xform, conj, [], data)
classic
data .map(item => item.val) .map(val => val + 2) .reduce((acc, val) => acc.length !== 0 && acc[acc.length - 1] === val ? acc : [...acc, val], []) .filter(isEven)
classic v2
data .map(item => item.val) .map(val => val + 2) .reduce((acc, val) => acc.length !== 0 && acc[acc.length - 1] === val ? acc : (acc.push(val) && acc), []) .filter(isEven)
xform2
transduce(xform, conjm, [], data)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
xform
classic
classic v2
xform2
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):
The provided benchmark measures the performance of three different approaches to filter and transform an array of objects in JavaScript: 1. **Transducers (xform)**: This approach is based on Ramda's transducer library, which provides a functional programming style for transforming and filtering data streams. The `xform` function composes multiple transformations together using the `R.compose` method. Pros: * Provides a declarative, functional programming style that can be easier to reason about and compose. * Can be more efficient than imperative approaches because it avoids the overhead of explicit loops. Cons: * May require additional setup and learning for users unfamiliar with functional programming concepts. * The transducer library may not be widely supported or optimized for performance in all browsers. 2. **Classic (classic)**: This approach uses the `map`, `filter`, and `reduce` methods provided by Array.prototype to transform and filter the data. It's a straightforward, imperative approach that's easy to understand but can be less efficient than the transducer approach due to its explicit loop structure. Pros: * Widely supported and well-known in JavaScript land. * Easy to understand and implement for users familiar with imperative programming. Cons: * Can be less efficient because of the overhead of explicit loops. * May not be as composable or declarative as the transducer approach. 3. **Classic v2 (classic v2)**: This variant of the classic approach uses `push` instead of `concat` to build up the result array. This can provide a small performance improvement because it avoids the overhead of creating a new array with `concat`. Pros: * Provides a slightly better performance profile than the original classic approach. * Still easy to understand and implement for users familiar with imperative programming. Cons: * Still has the same limitations as the original classic approach, including potential issues with browser support or performance in certain scenarios. In general, the choice of approach depends on the specific requirements and constraints of your project. If you need a high-performance, declarative solution that's easy to reason about, transducers might be the way to go. If you're working within a familiar imperative programming paradigm and want a straightforward solution with minimal learning curve, classic v2 could be a good choice. Other alternatives to consider: * **Array.prototype.forEach**: Instead of using `map`, `filter`, and `reduce`, you could use `forEach` as an alternative for the filtering step. This approach is generally less efficient than `filter` but can be useful in certain scenarios. * **Closure-based implementation**: You could implement a closure-based solution that avoids using Array.prototype methods altogether. This approach might provide better performance but requires more expertise and code complexity. Keep in mind that these alternatives are not necessarily part of the original benchmark and may not provide consistent or comparable results. The goal is to explore different approaches to achieve similar performance goals, so it's essential to evaluate each option based on its strengths and weaknesses for your specific use case.
Related benchmarks:
.map.filter vs transducers v3
RxJs vs Array
RxJs vs Array - fixed2
MapFilter (Native vs Ramda) 0.28
Comments
Confirm delete:
Do you really want to delete benchmark?