Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Created April 23, 2019 17:12
Show Gist options
  • Save ktilcu/eec22f4347e4d1c2f6841e1513524b91 to your computer and use it in GitHub Desktop.
Save ktilcu/eec22f4347e4d1c2f6841e1513524b91 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Iteration Samples - Ramda, Lodash, chaining, transducers">
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
<script src="https://cdn.rawgit.com/zloirock/core-js/master/client/shim.min.js"></script>
<script src="https://wzrd.in/standalone/tape@latest"></script>
<script src="https://wzrd.in/standalone/tap-browser-color@latest"></script>
<script src="https://wzrd.in/standalone/tape-catch@latest"></script>
</head>
<body>
<script>
window.test = tape;
tapBrowserColor();
</script>
<script id="jsbin-javascript">
'use strict';
var users = Array(10000000).fill().map(function (_, index) {
return {
name: 'User ' + (index + 1),
salary: Math.floor(Math.random() * 999999) + 100000
};
});
var getMaxEvenPaycheck = function getMaxEvenPaycheck(users) {
console.time('Method chaining');
var max = users.filter(function (user) {
return user.salary % 2 === 0;
}).map(function (user) {
return user.salary / 52;
}).reduce(function (a, b) {
return Math.max(a, b);
});
console.timeEnd('Method chaining');
return max;
};
var getMaxEvenPaycheckRamda = function getMaxEvenPaycheckRamda(users) {
console.time('Ramda');
var max = R.pipe(R.filter(function (user) {
return user.salary % 2 === 0;
}), R.map(function (user) {
return user.salary / 52;
}), R.reduce(R.max, 0))(users);
console.timeEnd('Ramda');
return max;
};
var getMaxEvenPaycheckLodash = function getMaxEvenPaycheckLodash(users) {
console.time('lodash');
var max = _.flow([_.filter(function (user) {
return user.salary % 2 === 0;
}), _.map(function (user) {
return user.salary / 52;
}), _.reduce(_.max, 0)])(users);
console.timeEnd('lodash');
return max;
};
var getMaxEvenPaycheckTransduce = function getMaxEvenPaycheckTransduce(users) {
console.time('Transduce');
var transducer = R.compose(R.filter(function (user) {
return user.salary % 2 === 0;
}), R.map(function (user) {
return user.salary;
}));
var max = R.reduce(R.max, 0, R.into([], transducer, users));
console.timeEnd('Transduce');
return max;
};
console.log(getMaxEvenPaycheck(users));
console.log(getMaxEvenPaycheckRamda(users));
console.log(getMaxEvenPaycheckLodash(users));
console.log(getMaxEvenPaycheckTransduce(users));
</script>
<script id="jsbin-source-javascript" type="text/javascript">const users = Array(10000000)
.fill()
.map((_, index) => ({
name: `User ${index + 1}`,
salary: Math.floor(Math.random() * 999999) + 100000
}))
const getMaxEvenPaycheck = (users) => {
console.time('Method chaining');
const max = users
.filter((user) => user.salary % 2 === 0)
.map((user) => user.salary / 52)
.reduce((a, b) => Math.max(a, b));
console.timeEnd('Method chaining');
return max;
};
const getMaxEvenPaycheckRamda = (users) => {
console.time('Ramda');
const max = R.pipe(
R.filter(user=> user.salary %2 === 0),
R.map(user=>user.salary/52),
R.reduce(R.max, 0)
)(users);
console.timeEnd('Ramda');
return max;
};
const getMaxEvenPaycheckLodash = (users) => {
console.time('lodash');
const max = _.flow([
_.filter(user=> user.salary %2 === 0),
_.map(user=>user.salary/52),
_.reduce(_.max, 0)
])(users);
console.timeEnd('lodash');
return max;
}
const getMaxEvenPaycheckTransduce = (users) => {
console.time('Transduce');
const transducer = R.compose(
R.filter(user=> user.salary %2 === 0),
R.map(user=>user.salary),
);
const max = R.reduce(R.max, 0, R.into([], transducer, users));
console.timeEnd('Transduce');
return max;
};
console.log(getMaxEvenPaycheck(users));
console.log(getMaxEvenPaycheckRamda(users));
console.log(getMaxEvenPaycheckLodash(users));
console.log(getMaxEvenPaycheckTransduce(users));</script></body>
</html>
'use strict';
var users = Array(10000000).fill().map(function (_, index) {
return {
name: 'User ' + (index + 1),
salary: Math.floor(Math.random() * 999999) + 100000
};
});
var getMaxEvenPaycheck = function getMaxEvenPaycheck(users) {
console.time('Method chaining');
var max = users.filter(function (user) {
return user.salary % 2 === 0;
}).map(function (user) {
return user.salary / 52;
}).reduce(function (a, b) {
return Math.max(a, b);
});
console.timeEnd('Method chaining');
return max;
};
var getMaxEvenPaycheckRamda = function getMaxEvenPaycheckRamda(users) {
console.time('Ramda');
var max = R.pipe(R.filter(function (user) {
return user.salary % 2 === 0;
}), R.map(function (user) {
return user.salary / 52;
}), R.reduce(R.max, 0))(users);
console.timeEnd('Ramda');
return max;
};
var getMaxEvenPaycheckLodash = function getMaxEvenPaycheckLodash(users) {
console.time('lodash');
var max = _.flow([_.filter(function (user) {
return user.salary % 2 === 0;
}), _.map(function (user) {
return user.salary / 52;
}), _.reduce(_.max, 0)])(users);
console.timeEnd('lodash');
return max;
};
var getMaxEvenPaycheckTransduce = function getMaxEvenPaycheckTransduce(users) {
console.time('Transduce');
var transducer = R.compose(R.filter(function (user) {
return user.salary % 2 === 0;
}), R.map(function (user) {
return user.salary;
}));
var max = R.reduce(R.max, 0, R.into([], transducer, users));
console.timeEnd('Transduce');
return max;
};
console.log(getMaxEvenPaycheck(users));
console.log(getMaxEvenPaycheckRamda(users));
console.log(getMaxEvenPaycheckLodash(users));
console.log(getMaxEvenPaycheckTransduce(users));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment