Skip to content

Instantly share code, notes, and snippets.

@wesleytodd
Created September 12, 2016 19:14
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 wesleytodd/03138614ee166b8971da2757d01b8028 to your computer and use it in GitHub Desktop.
Save wesleytodd/03138614ee166b8971da2757d01b8028 to your computer and use it in GitHub Desktop.
var map = require('lodash.map');
var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log('Lodash: ' + time(lodashMap, 1000));
console.log('Native: ' + time(nativeMap, 1000));
console.log('Loop: ' + time(loop, 1000));
function lodashMap () {
return map(data, function (i) {
return i + 1;
});
}
function nativeMap () {
return data.map(function (i) {
return i + 1;
});
}
function loop () {
var a = [];
for (var i = 0; i < data.length; i++) {
a[i] = data[i] + 1;
}
return a;
}
function time (fnc, times) {
var total = 0;
for (var i = 0; i < times; i++) {
var s = process.hrtime();
fnc();
var t = process.hrtime(s);
total += t[0] * 1e9 + t[1];
}
return total / times;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment