Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Measure the Performance of our Transducers

    Paul FrendPaul Frend

    Now that our transducer functions are functioning as expected, it’s time for another round of performance testing. We’ll use the same timeIt() function from Lesson One to see how our transducers compare to the Array built-in methods, an imperative solution, as well as the external transducer library transducers.js.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Let's begin by measuring the performance of some regular array operations using map and filter. We'll be doing all our operations on this rOfMillion, which is an array of one million random numbers.

    To begin with, we're just calling map and filter once. Then we're just doubling the amount of calls to map to get times two and times four down here. Now, let's try and run this. If we analyze our results, we see that every time we double our calls to map, the final time pretty much close to doubles every time.

    It scales linearly. Now, let's do the same operations, but with our new transducer helpers. We've got these stub timing methods down here, the first one being our imperative call, which just builds up the results a forEach loop.

    Then down here is where we'll fill out our transduce calls. Here, I'll do a call to seek. Just like we're above, I want this to be a combination of a map and a filter. I'll call compose, and we'll call filter is even, and map triplet.

    Our collection should be our array of million. Let's copy this. We'll paste that in down here, but double the calls to map, and do the same thing one last time. We want four calls here. Now, let's measure this again.

    To begin with, we see that our transduced call is pretty much exactly the same as our imperative call. All of our operations are now composed cleanly, and we can reuse them however we want. As we increase our calls to map, we see that these times are also much lower.

    Transducing with four calls to map is taking 74 milliseconds, compared with 1.6 seconds. Now, let's also do the same thing, but the transducers JS library. I'll uncomment this, and let's copy our call.

    If we go to the stop of the document, we're importing T from transducers-js. Now, the API of transducers-js is very similar to what we've built. T exposes lots of methods. To avoid name conflicts, we'll just access all of these methods of the T object.

    I'll go T.sequence, T.compose, T.filter, and The only other difference is that this collection needs to be the first argument. Let's put that up here, and that should be it. Let's run it again. If we look at the results, we can see it performs very similar to our implementation.