Created
September 27, 2016 10:05
-
-
Save mocheng/2b8e30ef03fb69f26f9a038e6199b71c to your computer and use it in GitHub Desktop.
transducer vs map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Rx = require('../../'); | |
const fs = require('fs'); | |
const t = require('transducers-js'); | |
const R = require('ramda'); | |
const _ = require('lodash'); | |
const mappingReducer = f => reducer => (result, input) => { | |
//console.log('enter mappingReducer', result, input); | |
return reducer(result, f(input)); | |
}; | |
const filterReducer = p => reducer => (result, input) => { | |
//console.log('enter filterReducer', result, input); | |
return p(input) ? reducer(result, input) : result; | |
}; | |
const transducer = R.compose(mappingReducer(x=>x+1), filterReducer(x=>x%2)); | |
//const transducer = R.compose(filterReducer(x=>x%2), mappingReducer(x=>x+1)); | |
const startTime1 = new Date(); | |
const source = _.range(1, 10000000); | |
const result1 = source.reduce(transducer((result, input) => {result.push(input);return result;}), []); | |
//console.log(s); | |
//s.map(x => console.log(x)); | |
const endTime1 = new Date(); | |
console.log('time : ' + (endTime1 - startTime1)); | |
const startTime2 = new Date(); | |
const result2 = source.map(x=>x+1).filter(x=>x%2); | |
const endTime2 = new Date(); | |
console.log('time : ' + (endTime2 - startTime2)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment