mirror of
https://github.com/samsonjs/batteries.git
synced 2026-04-27 15:07:42 +00:00
remove half-baked and experimental fuse stuff
This commit is contained in:
parent
435310ef38
commit
eb3f23a3e5
2 changed files with 0 additions and 173 deletions
121
fuse-test.js
121
fuse-test.js
|
|
@ -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);
|
|
||||||
}
|
|
||||||
52
lib/fuse.js
52
lib/fuse.js
|
|
@ -1,52 +0,0 @@
|
||||||
// batteries
|
|
||||||
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue