Skip to content

Instantly share code, notes, and snippets.

@jlongster
Last active April 16, 2017 07:49
Show Gist options
  • Save jlongster/d655170bcdc658d3eb0e to your computer and use it in GitHub Desktop.
Save jlongster/d655170bcdc658d3eb0e to your computer and use it in GitHub Desktop.
transducers w/immutable-js
# benchmark immutable.js with my transducers lib: https://github.com/jlongster/transducers.js
% node bench/immut.js
Immutable map/filter (1000) x 5,497 ops/sec ±2.63% (91 runs sampled)
transducer map/filter (1000) x 7,309 ops/sec ±0.73% (100 runs sampled)
Immutable map/filter (100000) x 62.73 ops/sec ±0.85% (67 runs sampled)
transducer map/filter (100000) x 80.15 ops/sec ±0.48% (71 runs sampled)
// benchmark immutable.js with my transducers lib: https://github.com/jlongster/transducers.js
var Benchmark = require('benchmark');
var t = require('../transducers');
var Immutable = require('immutable');
var suite = Benchmark.Suite('transducers');
function benchArray(n) {
var arr = new Immutable.Range(0, n).toVector();
suite
.add('Immutable map/filter (' + n + ')', function() {
arr.map(function(x) { return x + 10; })
.map(function(x) { return x * 2; })
.filter(function(x) { return x % 5 === 0; })
.filter(function(x) { return x % 2 === 0; })
.toVector();
})
.add('transducer map/filter (' + n + ')', function() {
Immutable.Vector.from(
t.seq(arr,
t.compose(
t.map(function(x) { return x + 10; }),
t.map(function(x) { return x * 2; }),
t.filter(function(x) { return x % 5 === 0; }),
t.filter(function(x) { return x % 2 === 0; })))
)
});
}
benchArray(1000);
benchArray(100000);
suite.on('cycle', function(event) {
console.log(String(event.target));
});
suite.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment