Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
var when1 = require('when1');
var when2 = require('when2');
when1.Promise.onPotentiallyUnhandledRejectionHandled = function(r) {
console.log('when1 Handled', r);
}
var rejectedPromise = when1.reject(new Error('Intentional failure'));
when2(rejectedPromise).done(undefined, function(error){
console.log('Rejection properly handled:', error);
'=>test': function(done) {
this.clock = this.useFakeTimers();
this.useFakeTimers();
setTimeout(done, 1000);
console.log(this.clock.timeouts);
this.clock.tick(5000);
assert(true);
},
return mopidy.tracklist.add(playlist.tracks).then(function (tlTracks) {
return mopidy.playback.play(tlTracks[trackNum]);
}).then(function() {
return mopidy.playback.getCurrentTrack().then(function (track) {
console.log("Now playing:", trackDesc(track));
});
})
@briancavalier
briancavalier / find-first.js
Created July 2, 2014 14:51
Simplified find left-most fulfilled
function findFirst(promises){
return recurseFindFirst(0, [], promises);
}
function recurseFindFirst(i, errors, promises) {
if(i === promises.length) {
var e = new Error('All promises rejected');
e.errors = errors;
return when.reject(e);
}
// JSON Patch containing a splice operation with context
[{
"op": "splice",
"path": "/3",
"+": ["d", "e"],
"-": ["x", "y", "z"],
"<": ["a", "b", "c"],
">": ["f", "g"]
}]
@briancavalier
briancavalier / mapWithKeys.js
Last active August 29, 2015 14:03
mapWithKeys
var keys = require('when/keys');
function mapWithKeys(object, f) {
return keys.all(Object.keys(object).reduce(function(o, k) {
o[k] = when(object[k], function(value) {
return f(value, k);
});
return o;
}, {}));
}
module.exports = curry;
function curry (f) {
var arity = f.length;
var params = [];
var end = createEnd(f, arity);
return createCurried(params, arity, end);
}
function createEnd (f, arity) {

This is a simple micro-benchmark of four ways to curry JavaScript functions. The benchmark focuses on measuring the performance of the resulting curried functions, not on the performance of currying the functions in the first place. That'd also be an interesting comparison, especially since two of these approaches rely on dynamic compilation at curry-time. Typically, though, functions should be curried once at load/init/startup time, so the runtime performance of the resulting curried functions is typically more important to the overall application.

Note: I'm not a micro-benchmarking expert, so I might be doing something dumb! If you see a problem, please suggest a fix :)

Approaches

unscriptable dynamic compile

This one was taken from @unscriptable's gist that uses dynamic compilation to generate the final function in the curry chain.

var when = require('when');
when.promise(function(resolve) {
setTimeout(resolve, 100);
}).then(function() {
return when.promise(function(resolve) {
setTimeout(resolve, 100);
})
}).then(function() {
return when.promise(function(resolve) {