From eb3f23a3e56f22a51c0b4a8b5191d784791c22b2 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 5 Jun 2011 21:43:23 -0700 Subject: [PATCH] remove half-baked and experimental fuse stuff --- fuse-test.js | 121 --------------------------------------------------- lib/fuse.js | 52 ---------------------- 2 files changed, 173 deletions(-) delete mode 100644 fuse-test.js delete mode 100644 lib/fuse.js diff --git a/fuse-test.js b/fuse-test.js deleted file mode 100644 index 28cacd6..0000000 --- a/fuse-test.js +++ /dev/null @@ -1,121 +0,0 @@ -var fuse = require('./lib/fuse') - , a = [] - , n = Number(process.argv[2]) || 10000000 // 10,000,000 - , iters = a.length - , parts = [] - , s - , start - ; - -iters = n; -while (iters >= 1) { - s = (iters % 1000).toString(); - if (iters / 1000 >= 1) while (s.length < 3) s = '0' + s; - parts.push(s); - iters = iters / 1000; -} -console.log(parts.reverse().join(',') + ' iterations'); -while (n--) a.push(n); - -function time(title, fn, cb) { - console.log('---- ' + title + ' ----'); - var i = 0 - , n = 5 - , start - , avg = 0 - , min - , max = 0 - , next = function() { - start = +new Date(); - fn(function() { - var time = +new Date() - start; - if (time > max) max = time; - if (!min || time < min) min = time; - avg += time; - if (++i < n) next(); - else done(); - }); - } - , done = function() { - avg /= n; - console.log('avg: ' + avg + 'ms'); - console.log('min: ' + min + 'ms'); - console.log('max: ' + max + 'ms'); - console.log(); - } - next(); -} - -function timeSync(title, fn, cb) { - console.log('---- ' + title + ' ----'); - var i = 0 - , n = 5 - , start - , avg = 0 - , min - , max = 0 - ; - for (; i < n; ++i) { - start = +new Date(); - fn(); - var time = +new Date() - start; - if (time > max) max = time; - if (!min || time < min) min = time; - avg += time; - } - avg /= n; - console.log('avg: ' + avg + 'ms'); - console.log('min: ' + min + 'ms'); - console.log('max: ' + max + 'ms'); - console.log(); -} - -// Plain old while loop -timeSync('while loop', function() { - var b = [] - , i = a.length - ; - while (i--) { - b[i] = (a[i] + 1) * 2; - for (var j = 0; j < 100; ++j) j; - } -}); - -// Composed map -timeSync('composed map', function() { - a.map(function(x) { - for (var j = 0; j < 100; ++j) j; - return (x + 1) * 2; - }); -}); - -// Chained map (modular) -timeSync('chained map', function() { - a.map(add1).map(wasteTime).map(times2); -}); - -// Synchronous fused map -timeSync('fused map (sync)', function() { - fuse.fusedMapSync(add1, wasteTime, times2)(a); -}); - -// Asynchronous fused map (test not actually async, but meh) -time('fused map (async)', function(cb) { - fuse.fusedMap(add1Async, wasteTimeAsync, times2Async)(a, function(b) { - cb(); - }); -}); - -function add1(v) { return v + 1; } -function times2(v) { return v * 2; } -function wasteTime(v) { - for (var i = 0; i < 100; ++i) i; - return v; -} - -function add1Async(v, cb) { cb(v + 1); } -function times2Async(v, cb) { cb(v * 2); } -function wasteTimeAsync(v, cb) { - for (var i = 0; i < 100; ++i) i; - cb(v); -} diff --git a/lib/fuse.js b/lib/fuse.js deleted file mode 100644 index e0a3c8d..0000000 --- a/lib/fuse.js +++ /dev/null @@ -1,52 +0,0 @@ -// batteries -// Copyright 2010 - 2011 Sami Samhuri - -// TODO non-enumerable properties -exports.extendArray = function() { - Array.prototype.fusedMap = function() { - var args = [].slice.call(arguments); - return function(cb) { - fusedMap.apply(null, args)(this, cb); - } - }; - Array.prototype.fusedMapSync = function() { - return fusedMapSync.apply(null, arguments)(this); - }; -}; - -exports.fusedMap = fusedMap; -function fusedMap() { - var fns = Array.isArray(arguments[0]) ? arguments[0] : [].slice.call(arguments); - return function(a, cb) { - var b = [] - , n = a.length - ; - a.forEach(function(v, i) { - var nFns = fns.length - , next = function(j) { - if (j < nFns) - fns[j](v, function(v) { next(j + 1); }) - else - done(); - } - , done = function() { - b[i] = v; - if (--n === 0) cb(b); - } - next(0); - }) - } -} - -exports.fusedMapSync = fusedMapSync; -function fusedMapSync() { - var fns = Array.isArray(arguments[0]) ? arguments[0] : [].slice.call(arguments) - , n = fns.length - ; - return function(a) { - return a.map(function(v) { - for (var i = 0; i < n; ++i) v = fns[i](v); - return v; - }); - }; -}