Coder Social home page Coder Social logo

ryanpcmcquen / flatmap-fast Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 2.0 31 KB

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

Home Page: https://www.npmjs.com/package/flatmap-fast

License: Mozilla Public License 2.0

JavaScript 100.00%
hacktoberfest flatmap

flatmap-fast's Introduction

Gitpod ready-to-code

flatmap-fast

The fastest flatMap this side of node.

Takes two arguments:

  1. An array.
  2. A callback function (optional).
const flatMap = require("flatmap-fast");

const testArr = ['Hi', 'World'];
const splitWord = (word) => word.split('');

flatMap(testArr, splitWord);

// => ['H', 'i', 'W', 'o', 'r', 'l', 'd']

flatMap([1, 2, 3, 4], (x) => [x, x * 2]);

// => [1, 2, 2, 4, 3, 6, 4, 8]

Run npm test to test this flatMap against other flatMaps.

$ node --version
v12.18.3
$ yarn test
yarn run v1.22.4
$ node test.js
// => flatMapFast took: 650.86651 milliseconds.
[
  'H', 'i', 'W',
  'o', 'r', 'l',
  'd'
]
// => flatmapjs took: 667.361729 milliseconds.
[
  'H', 'i', 'W',
  'o', 'r', 'l',
  'd'
]
// => flatMapFast took: 517.463478 milliseconds.
[
  1, 2, 2, 4,
  3, 6, 4, 8
]
// => flatmapjs took: 676.208413 milliseconds.
[
  1, 2, 2, 4,
  3, 6, 4, 8
]
Done in 2.74s.

flatmap-fast's People

Contributors

ryanpcmcquen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

linusu dadiborn

flatmap-fast's Issues

wrong insertion

on line #13 change result.push(arr) to result.push(arr[outerIndex])

flatMap should map before flatten

The flatMap operator is commonly defined as map followed by flatten. This module uses flatten(a).map(f) where it should instead be flatten(a.map(f)).

After making this change I get faster results for flatmapjs (using node v6.9.2):

$ node test.js 
// => flatMapFast took: 1548.439797 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
// => flatMapConcatApply took: 1426.5782669999999 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
// => flatMapConcatRest took: 1620.313789 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
// => flatmapjs took: 624.206646 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
'use strict';
const arrNum = [1, 2, 3];
const flatten = (a) => a.reduce((x, y) => x.concat(y));
const flatMapFast = (a, f) => flatten(a.map(f));
const flatMapConcatApply = (a, f) => Array.prototype.concat.apply([], a.map(f));
// Note that this version requires the `--harmony` flag.
const flatMapConcatRest = (a, f) => [].concat(...a.map(f));

/* 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');
const fn = (i) => [i, i + i];

testPerf("flatMapFast", flatMapFast, arrNum, fn);
console.log(flatMapFast(arrNum, fn));

testPerf("flatMapConcatApply", flatMapConcatApply, arrNum, fn);
console.log(flatMapConcatApply(arrNum, fn));

// Note that this version requires the `--harmony` flag.
testPerf("flatMapConcatRest", flatMapConcatRest, arrNum, fn);
console.log(flatMapConcatRest(arrNum, fn));

testPerf("flatmapjs", flatmapjs, arrNum, fn);
console.log(flatmapjs(arrNum, fn));

Looking just at flatten, it seems (a) => Array.prototype.concat.apply([], a) is fastest.

$ node flatten.js 
// => flattenReduce took: 749.408898 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
// => flattenConcatApply took: 641.470689 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
// => flattenConcatRest took: 944.365671 milliseconds.
[ 1, 2, 2, 4, 3, 6 ]
'use strict';
const arrNum = [[1,2], [2,4], [3,6]];
const flattenReduce = (a) => a.reduce((x, y) => x.concat(y));
const flattenConcatApply = (a) => Array.prototype.concat.apply([], a);
// Note that this version requires the `--harmony` flag.
const flattenConcatRest = (a) => [].concat(...a);

const testPerf = require('testperf');

testPerf("flattenReduce", flattenReduce, arrNum);
console.log(flattenReduce(arrNum));

testPerf("flattenConcatApply", flattenConcatApply, arrNum);
console.log(flattenConcatApply(arrNum));

// Note that this version requires the `--harmony` flag.
testPerf("flattenConcatRest", flattenConcatRest, arrNum);
console.log(flattenConcatRest(arrNum));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.