From 6d407d43c1f36ef83cb11da1166611ae79296ac3 Mon Sep 17 00:00:00 2001 From: buzzdecafe Date: Fri, 4 Jul 2014 16:17:26 +0000 Subject: [PATCH] filter and foldl benchmark suites --- .gitignore | 1 + bench/filter.bench.js | 36 ++++++++++++++++++++++++++++++++++++ bench/foldl.bench.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 bench/filter.bench.js create mode 100644 bench/foldl.bench.js diff --git a/.gitignore b/.gitignore index 49e7dc26b..b7f397565 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ node_modules coverage *.swp + /node_modules /examples/math.html /examples/math.js diff --git a/bench/filter.bench.js b/bench/filter.bench.js new file mode 100644 index 000000000..760b41d65 --- /dev/null +++ b/bench/filter.bench.js @@ -0,0 +1,36 @@ +var _ = require('lodash'); +var filter = require('../ramda').filter; +var Benchmark = require('benchmark'); +var fs = require('fs'); +var out = './report/filter-' + (new Date()).toISOString() + '.json'; +var suite = new Benchmark.Suite; + +var nums = [8,2,85,2,34,3,23,247,57,8,0,6,5,46,54,643]; +function isEven(x) { return x % 2 === 0; } +var filterEven = filter(isEven); + +suite +.add('_.filter(nums, isEven)', function() { + _.filter(nums, isEven); +}) +.add('filter(isEven, nums)', function() { + filter(isEven, nums); +}) +.add('filter(isEven)(nums)', function() { + filter(isEven)(nums); +}) +.add('filterEven(nums)', function() { + filterEven(nums); +}) +.on('cycle', function(event) { + console.log(String(event.target)); +}) +.on('complete', function() { + console.log('Fastest is ' + this.filter('fastest').pluck('name')); + fs.writeFile(out, JSON.stringify(this), function(err) { + if (err) { console.log('failed to write ' + out); } + else { console.log('saved ' + out); } + }); +}) +.run(); + diff --git a/bench/foldl.bench.js b/bench/foldl.bench.js new file mode 100644 index 000000000..d78c11eb7 --- /dev/null +++ b/bench/foldl.bench.js @@ -0,0 +1,36 @@ +var _ = require('lodash'); +var foldl = require('../ramda').foldl; +var Benchmark = require('benchmark'); +var fs = require('fs'); +var out = './report/foldl-' + (new Date()).toISOString() + '.json'; +var suite = new Benchmark.Suite; + +var nums = [8,2,85,2,34,3,23,247,57,8,0,6,5,46,54,643]; +function add(acc, x) { return acc + x; } +var foldlAdd = foldl(add, 0); + +suite +.add('_.reduce(nums, add, 0)', function() { + _.reduce(nums, add, 0); +}) +.add('foldl(add, 0, nums)', function() { + foldl(add, 0, nums); +}) +.add('foldl(add, 0)(nums)', function() { + foldl(add, 0)(nums); +}) +.add('foldlAdd(nums)', function() { + foldlAdd(nums); +}) +.on('cycle', function(event) { + console.log(String(event.target)); +}) +.on('complete', function() { + console.log('Fastest is ' + this.filter('fastest').pluck('name')); + fs.writeFile(out, JSON.stringify(this), function(err) { + if (err) { console.log('failed to write ' + out); } + else { console.log('saved ' + out); } + }); +}) +.run(); +