Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / 0-info.md
Last active August 29, 2015 14:11
most.js representative results
var jiff = require('jiff');
var p = [{"op":"test","path":"/messages/0","value":{"id":"QJ951IQ-","time":"2014-12-05T02:24:31.880Z","userId":"7yjrlN0x","text":"q","thread":"default","isThread":false,"user":{"name":"chen","avatar":"http://tp1.sinaimg.cn/1651843872/180/40048616024/1","nickname":"chen","thread":"default","id":"7yjrlN0x","online":true}}},{"op":"remove","path":"/messages/0"},{"op":"add","path":"/messages/2","value":{"id":"71D0JL7b","time":"2014-12-05T02:25:33.729Z","userId":"7JHrWpG-","text":"r","thread":"default","isThread":false,"user":{"name":"yong","avatar":"http://tp3.sinaimg.cn/1346257214/50/1269085478/0","nickname":"yong","id":"7JHrWpG-","thread":"default","online":true}}}];
var d1 = {"messages":[{"id":"QJ951IQ-","time":"2014-12-05T02:24:31.880Z","userId":"7yjrlN0x","text":"q","thread":"default","isThread":false,"user":{"name":"chen","avatar":"http://tp1.sinaimg.cn/1651843872/180/40048616024/1","nickname":"chen","thread":"default","id":"7yjrlN0x","online":true}},{"id":"mJR6kUmW","time":"201
function AbortablePromise(resolver) {
if (typeof resolver !== 'function')
throw new Error('AbortablePromise needs a resolver function');
var abort;
var promise = new Promise(function (resolve, reject) {
var aborter;
abort = function () {
if (aborter == null)
var switchLatest = require('./lib/combinator/switch').switch;
exports.switch = exports.switchLatest = switchLatest;
Stream.prototype.switch = Stream.prototype.switchLatest = function() {
return switchLatest(this);
};
@briancavalier
briancavalier / clone.js
Created December 3, 2014 16:26
Recursively clone a JS Object or Array
module.exports = clone;
function clone(x) {
if(x == null || typeof x !== 'object') {
return x;
}
if(Array.isArray(x)) {
return cloneArray(x);
}
var most = require('../../most');
//var n = 0;
//var n = 1;
var n = 2;
var s = most.of(1).flatMap(function() {
return most.periodic(1000);
}).take(n);
var most = require('../../most');
var n = 0;
//var n = 1;
//var n = 2;
var s = most.periodic(1000).take(n);
s.observe(function(x) {
console.log(x);
@briancavalier
briancavalier / 0-reduce-no-sparse-array.js
Last active August 29, 2015 14:08
Improve reduce perf, non-sparse arrays
// Dropping sparse array support makes this code much simpler
function reduce(promises, f) {
var hasArg = arguments.length > 2;
if((promises.length>>>0) === 0) {
return hasArg ? when.promise(arguments[2]) : when.reject(new TypeError('Cannot reduce empty array with no initial value'));
}
return hasArg ? runReduce(0, promises, f, arguments[2])
: runReduce(1, promises, f, promises[0]);
}
@briancavalier
briancavalier / 0-reduce.js
Created November 3, 2014 02:30
Improve reduce perf
var when = require('../../../when');
var a = [];
for(var i=0; i<1000000; ++i) {
a[i] = when.resolve(i);
}
var start = Date.now();
when.reduce(a, add, 0).done(function(r) {
//when.reduceRight(a, add, 0).done(function(r) {
@briancavalier
briancavalier / 0-test.js
Created November 3, 2014 01:09
when.map perf
var when = require('../../../when');
var a = [];
for(var i=0; i<1000000; ++i) {
a[i] = when.resolve(i);
}
var start = Date.now();
when.map(a, function(x) {
return add(x, 1);