{"id":30677160,"url":"https://github.com/ajitjha393/js-transducers","last_synced_at":"2025-09-01T11:12:32.941Z","repository":{"id":63069882,"uuid":"564253059","full_name":"ajitjha393/js-transducers","owner":"ajitjha393","description":"The Functional transducers that allow transformations with improved performance","archived":false,"fork":false,"pushed_at":"2022-11-12T07:23:55.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-23T22:39:33.943Z","etag":null,"topics":["compose","functional-programming","transducers"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajitjha393.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-10T10:20:48.000Z","updated_at":"2023-03-17T13:26:00.000Z","dependencies_parsed_at":"2022-11-12T07:30:26.224Z","dependency_job_id":null,"html_url":"https://github.com/ajitjha393/js-transducers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ajitjha393/js-transducers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajitjha393%2Fjs-transducers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajitjha393%2Fjs-transducers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajitjha393%2Fjs-transducers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajitjha393%2Fjs-transducers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajitjha393","download_url":"https://codeload.github.com/ajitjha393/js-transducers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajitjha393%2Fjs-transducers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273113749,"owners_count":25048239,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compose","functional-programming","transducers"],"created_at":"2025-09-01T11:12:20.382Z","updated_at":"2025-09-01T11:12:32.909Z","avatar_url":"https://github.com/ajitjha393.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js-transducers\n\nThe Functional and Declarative Implementation of `map`, `reduce`, `filter` were added to JS in ES2015 to `Array.Prototype`.\n\nSince then we use them to perform all sorts of operations, filtering, and transformation declaratively on Array for manipulating data.\n\nBut all these operations are independent of each other, which means we produce\nan intermediate resultant array which is passed as an input and we loop over it again in the next chained operation.\n\n\n```js \ntimeIt('million - chained', () =\u003e {\n    arrOfMillion\n      .map(multiplyByThree)\n      .filter(isEven);\n  });\n\ntimeIt('million - chained x2', () =\u003e {\n  arrOfMillion\n    .map(multiplyByThree)\n    .map(multiplyByThree)\n    .filter(isEven);\n});\n\n```\n\nThis implementation and existing behavior works really well in our normal day to day usage involving very smaller amount of data values.\nBut what if we need to perform these operations and transformations multiple times over `millions` of data values.\n\nWe see a significant performance degradation.\n\n\n## Imperative Solution\n\nNow we can go back to the imperative solution but will loose all the benefits that `Declarative` and `Functional Programming` provided us.\n\nThe Truthness and Referentiality of the codebase will not be maintained \n\n```js\ntimeIt('million - imperative', () =\u003e {\n  const result = [];\n  arrOfMillion\n    .forEach(val =\u003e {\n      const tripled = tripleIt(val);\n      if (isEven(tripled)) result.push(tripled);\n    });\n});\n```\n\nIs there a way where I can get best of both the worlds?\n\nAnd do not have to tradeoff on performance?\n\nAnd the answer is YES!  - ***Using transducers***\n\n\n## Transducer Solution\n\nI implemented these transducers and other functional adapter and helpers - `into` and `seq`\n\nUsing this utiltiy we can rewrite the above mentioned code as - \n\n \n\n```js\n\ntimeIt('million - transduce', () =\u003e {\n  seq(\n    compose(\n      map(multiplyByThree),\n      filter(isEven),\n    ),\n    arrOfMillion,\n  );\n});\n \n\ntimeIt('million - transduce x2', () =\u003e {\n  seq(\n    compose(\n      map(multiplyByThree),\n      map(multiplyByThree),\n      filter(isEven),\n    ),\n    arrOfMillion,\n  );\n});\n \n\ntimeIt('million - transduce x4', () =\u003e {\n  seq(\n    compose(\n      map(multiplyByThree),\n      map(multiplyByThree),\n      map(multiplyByThree),\n      map(multiplyByThree),\n      filter(isEven),\n    ),\n    arrOfMillion,\n  );\n});\n\n```\n\n## Measuring the Performance of our Transducers\n\nAs it is evident from the metric, the transducers allow us to perform all these operations and transformations in a much performant way\nwhen dealing with huge amount of data and values that we want to iterate over.\n\nIt also enables us with more flexibility over the type of collection as it is agnostic to the container and not limited to only using Arrays.\n\n\n![image](https://user-images.githubusercontent.com/42679346/201461654-fb4ab017-2da0-4c8a-8fb4-c2c84fc83888.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajitjha393%2Fjs-transducers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajitjha393%2Fjs-transducers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajitjha393%2Fjs-transducers/lists"}