-
-
Save fogus/79d06510f1841d6857c3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
if(typeof require != "undefined") { | |
var lodash = require("lodash"), | |
_ = require("underscore"); | |
} | |
var MapTransformer = function(f, nextTransformer) { | |
this.f = f; | |
this.nextTransformer = nextTransformer; | |
}; | |
MapTransformer.prototype.init = function() { | |
return this.nextTransformer.init(); | |
}; | |
MapTransformer.prototype.result = function(result) { | |
return this.nextTransformer.result(result); | |
}; | |
MapTransformer.prototype.step = function(result, input) { | |
return this.nextTransformer.step(result, this.f(input)); | |
}; | |
var MapTransducer = function(f) { | |
this.f = f; | |
}; | |
MapTransducer.prototype.transformer = function(nextTransformer) { | |
return new MapTransformer(this.f, nextTransformer); | |
}; | |
var transduce = function(transducer, builder, init, array) { | |
var accumulator = init, | |
transformer = transducer.transformer(builder); | |
for(var i = 0; i < array.length; i++) { | |
accumulator = transformer.step(accumulator, array[i]); | |
}; | |
return transformer.result(accumulator); | |
}; | |
var ArrayTransformer = function() {}; | |
ArrayTransformer.prototype.result = function(array) { | |
return array; | |
}; | |
ArrayTransformer.prototype.step = function(array, value) { | |
array.push(value); | |
return array; | |
}; | |
var inc = function(n) { | |
return n + 1; | |
}; | |
function log() { | |
if(typeof console != "undefined") { | |
console.log.apply(console, arguments); | |
} else { | |
print(Array.prototype.slice.call(arguments, 0).join(" ")); | |
} | |
} | |
function time(desc, f) { | |
var s = new Date(); | |
var ret = f(); | |
log(desc, ret, (new Date())-s, "milliseconds elapsed"); | |
}; | |
var smallArray = [0,1,2,3,4,5,6,7,8,9]; | |
log(smallArray.map(inc)); | |
log(transduce(new MapTransducer(inc), new ArrayTransformer(), [], smallArray)); | |
var largeArray = []; | |
for(var i = 0; i < 1000000; i++) { | |
largeArray.push(i); | |
} | |
time("for loop", function() { | |
var ret = []; | |
for(var i = 0; i < largeArray.length; i++) { | |
ret.push(inc(largeArray[i])); | |
} | |
return ret.length; | |
}); | |
time("native map", function() { | |
return largeArray.map(inc).length; | |
}); | |
time("transducer", function() { | |
return transduce(new MapTransducer(inc), new ArrayTransformer(), [], largeArray).length; | |
}); | |
if(typeof lodash != "undefined") { | |
time("lodash map", function() { | |
return lodash.map(largeArray, inc).length; | |
}); | |
} | |
if(typeof _ != "undefined") { | |
time("underscore map", function() { | |
return _.map(largeArray, inc).length; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment