UNPKG

flatmap-fast

Version:

A fast and modern flatMap for node. Monads for the win!

44 lines (35 loc) 1.12 kB
'use strict'; const testArr = ['Hi', 'World']; const splitWord = (word) => word.split(''); const flatMapFast = require('./index.js'); /* https://www.npmjs.com/package/flatmap v0.0.3 */ const flatmapjs = function (arr, iter, context) { var results = []; if (!Array.isArray(arr)) return results; arr.forEach(function (value, index, list) { var res = iter.call(context, value, index, list); if (Array.isArray(res)) { results.push.apply(results, res); } else if (res != null) { results.push(res); } }); return results; }; const testPerf = require('testperf'); testPerf("flatMapFast", flatMapFast, testArr, splitWord); console.log( flatMapFast(testArr, splitWord) ); testPerf("flatmapjs", flatmapjs, testArr, splitWord); console.log( flatmapjs(testArr, splitWord) ); testPerf("flatMapFast", flatMapFast, [1, 2, 3, 4], (x) => [x, x * 2]); console.log( flatMapFast([1, 2, 3, 4], (x) => [x, x * 2]) ); testPerf("flatmapjs", flatmapjs, [1, 2, 3, 4], (x) => [x, x * 2]); console.log( flatmapjs([1, 2, 3, 4], (x) => [x, x * 2]) );