Measure the Performance of our Transducers

Paul Frend
InstructorPaul Frend
Share this video with your friends

Social Share Links

Send Tweet

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.

Instructor: [00:00] 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.

[00:12] 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.

[00:32] 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.

[00:47] 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.

[01:06] 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.

[01:23] 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.

[01:39] 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.

[01:55] 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.

[02:12] I'll go T.sequence, T.compose, T.filter, and T.map. 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.