Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.map.filter vs transducers v3
(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):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The benchmark defines three different approaches to filter and transform an array of random numbers: 1. **Transducers**: A transducer is a higher-order function that takes a reducing function and returns a new function that combines the two. In this case, we have `xform`, which is composed of multiple functions: * `mapping`: applies a transformation to each element * `filtering`: filters out elements based on a predicate * `uniq`: removes duplicates 2. **Classic**: A simple filter and map approach using the `map` method, followed by `reduce` and `filter`. 3. **Classic V2**: Similar to Classic, but uses `push` instead of `conj` (a custom array concatenation function) for adding elements. **Options Compared** The benchmark compares the performance of these three approaches: * Transducers (specifically, the `xform` function) * Classic (with `map`, `reduce`, and `filter`) * Classic V2 (with `push` instead of `conj`) **Pros and Cons** Here are some pros and cons for each approach: 1. **Transducers (xform)**: * Pros: Composes multiple functions, making it easy to chain transformations. * Cons: Can be slower due to the overhead of function calls and composition. 2. **Classic**: Pros: Simple and straightforward implementation. Cons: Can be slower due to repeated array mutations (`push` or `conj`) and function calls. 3. **Classic V2**: * Pros: Uses `push`, which is a more efficient way to add elements to an array than `conj`. * Cons: Still uses repeated array mutations, which can lead to performance issues. **Library** The benchmark uses the Ramda library (version 0.27.0) for functional programming utilities, including `map`, `filter`, and `reduce`. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. The code relies on standard JavaScript libraries like Ramda and built-in methods like `map`, `reduce`, and `filter`. **Other Alternatives** If you want to explore other alternatives, here are a few: * Use built-in JavaScript methods: Instead of using transducers or custom functions, you can use built-in methods like `filter()` and `map()`. * Use array iterators: You can implement your own iterator using the `for...of` loop and array methods. * Use libraries like Lodash or underscore.js: These libraries provide a comprehensive set of functional programming utilities that might be faster than Ramda. Keep in mind that performance optimization is often dependent on the specific use case, hardware, and software configuration. The best approach may vary depending on your specific needs and requirements.
Related benchmarks:
.map.filter vs transducers v2
RxJs vs Array
RxJs vs Array - fixed2
MapFilter (Native vs Ramda) 0.28
Comments
Confirm delete:
Do you really want to delete benchmark?