, , . https://jsperf.com/filter-than-map-or-reduce/1
.

NodeJS ( npm i benchmark)
var suite = new (require('benchmark')).Suite
function getSampleInput() {
return [{file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}, {file: 'foo'}, {other: 'bar'}, {file: 'baz'}, {file: 'quux'}, {other: 'quuxdoo'}, {file: 'foobar'}]
}
function reduce(results) {
return results.reduce(
(acc, result) => result.file ? acc.concat([result.file]) : acc ,
[]
)
}
function filterThanMap(results) {
return results.filter(function(r){
return r.file;
})
.map(function(r){
return r.file;
});
}
function forEach(results) {
const files = [];
results.forEach(function(r){
if(r.file) files.push(r.file);
});
return files
}
suite
.add('filterThanMap', function() {filterThanMap(getSampleInput())})
.add('reduce', function() {reduce(getSampleInput())})
.add('forEach', function() {forEach(getSampleInput())})
.on('complete', function() {
console.log('results:')
this.forEach(function(result) {
console.log(result.name, result.count, result.times.elapsed)
})
console.log('the fastest is', this.filter('fastest').map('name')[0])
})
.run()