Amrishkhan Sheik Abdullah Posted on May 30 Transducers in JavaScript: When `reduce()` Is Not the End of the Story # typescript # webdev # javascript # programming In my previous article, I wrote about one of the most underestimated functions in JavaScript: reduce() . That article triggered a very fair discussion. Some developers agreed that reduce() is powerful. Some pushed back. And honestly, the pushback was valid. Because reduce() is one of those functions that can be either beautiful or terrible depending on how it is used. A simple reduce() can express a clean state transformation. A bad reduce() can become unreadable very quickly. So before talking about transducers, let me say this clearly: I do not think reduce() should replace simple loops everywhere. In many cases, a plain for...of loop is more readable, more debuggable, and often faster. const result = [] for ( const user of users ) { if ( user . active ) { result . push ( user . name ) } } Enter fullscreen mode Exit fullscreen mode This is not “less functional”. This is just clear code. And clarity matters. But there is another interesting idea hiding behind reduce() . That idea is not just: Use reduce() more. The deeper idea is: What if reducers themselves could be composed? That is where transducers come in. The Problem With map().filter().reduce() Let’s start with a normal JavaScript pipeline. const result = users . filter ( user => user . active ) . map ( user => user . name ) . filter ( name => name . length > 3 ) Enter fullscreen mode Exit fullscreen mode This is readable. But internally, JavaScript creates intermediate arrays. users → filtered users array → mapped names array → filtered names array Enter fullscreen mode Exit fullscreen mode For small arrays, this is perfectly fine. For UI code, this is usually fine. But for large data pipelines, logs, analytics, imports, file processing, or repeated transformations, this can create unnecessary work. You could rewrite it using reduce()
Back to Home

Transducers in JavaScript: When `reduce()` Is Not the End of the Story
B
Blizine Admin
·2 min read·0 views
📰Dev.to — dev.to
B
Blizine Admin
View Profile Staff Writer