Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/9813103 to your computer and use it in GitHub Desktop.
Save alexbeletsky/9813103 to your computer and use it in GitHub Desktop.
Which is better
// which is better?
function f() {
// returns object.. takes some cpu to complete
}
// this one?
return f().one || f().two;
// that one?
var p = f();
return p.one || p.two;
// or that one?
return either(fc(), 'one', 'two');
function either() {
var o = arguments[0];
var p = Array.prototype.slice.call(arguments, 1);
return p.reduce(function (p, c) {
return o[p] || o[c];
});
}
var titles = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_TITLE', content: either(_.first(grouped[key]), 'title', 'authorName') };
});
var descriptions = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_DESCRIPTION', content: _.first(grouped[key]).description };
});
function either() {
var o = arguments[0];
var p = Array.prototype.slice.call(arguments, 1);
return p.reduce(function (p, c) {
return o[p] || o[c];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment