Created
November 27, 2016 17:52
-
-
Save Tom910/3e018ca38979f130ae6d6fd266ff76d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var expect = require('expect'); | |
var _ = require('lodash'); | |
var R = require('ramda'); | |
var suite = new Benchmark.Suite; | |
var testArray = [ | |
{ items: [{status: 'new', payload: [1, 2, 3]}, {status: 'old'}, {status: 'new', payload: [2, 1, 4]}]}, | |
{ items: [{status: 'new', payload: [8, 2, 7]}, {status: 'new', payload: [0, 2, 9]}, {status: 'old'}]}, | |
{ items: [{status: 'old'}, {status: 'new', payload: [5, 2, 3]}, {status: 'new', payload: [1, 4, 3]}]} | |
]; | |
var resultat = [[[1,2,3],[2,1,4]],[[8,2,7],[0,2,9]],[[5,2,3],[1,4,3]]]; | |
suite | |
.add('es3', () => es3(testArray)) | |
.add('es5', () => es5(testArray)) | |
.add('lodash', () => lodash(testArray)) | |
.add('ramda', () => ramda(testArray)) | |
.on('cycle', event => console.log(String(event.target))) | |
.on('complete', function() {console.log('Fastest is ' + this.filter('fastest').map('name'))}) | |
.run({ 'async': true }); | |
function es3(array) { | |
var length = array.length, | |
result = Array(length), | |
i = 0; | |
for(; i < length; i++) { | |
var items = array[i].items, | |
lengthItems = items.length, | |
payload = [], | |
j = 0; | |
for (; j < lengthItems; j++) { | |
if (items[j].status === 'new') { | |
payload.push(items[j].payload); | |
} | |
} | |
result[i] = payload; | |
} | |
return result; | |
} | |
function es5(arr) { | |
return arr.map(function(item) { | |
var result = []; | |
item.items.forEach(function (d) { if (d.status === 'new') { result.push(d.payload); } }); | |
return result; | |
}) | |
} | |
function lodash(arr) { | |
return _.map(arr, function (item) { | |
var result = []; | |
_.forEach(item.items, function (d) { if (d.status === 'new') { result.push(d.payload); } }); | |
return result; | |
}) | |
} | |
function ramda(arr) { | |
return R.compose( | |
R.map(R.map(R.prop('payload'))), | |
R.map(R.filter(R.propEq('status', 'new'))), | |
R.map(R.prop('items')) | |
)(arr); | |
} | |
expect(es3(testArray)).toEqual(resultat); | |
expect(es5(testArray)).toEqual(resultat); | |
expect(lodash(testArray)).toEqual(resultat); | |
expect(ramda(testArray)).toEqual(resultat); | |
console.log('Тесты прошли'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment