Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split _reduce to _xReduce(for transformers) & _reduce(for reducers) #3248

Merged
merged 2 commits into from
Mar 28, 2022

Conversation

xgbuils
Copy link
Contributor

@xgbuils xgbuils commented Feb 27, 2022

Following the approach about what I said in #3247, I also extracted _arrayReduce and _xArrayReduce to be used in places where function are only working with array lists. In this way, we are not executing additional type checks in cases are not needed.

@xgbuils
Copy link
Contributor Author

xgbuils commented Feb 27, 2022

Notice that this PR doesn't colide with #2767. #2767 is a behavior change. This PR is just a refactor.

If we merge this PR and we want to apply the behavior of #2767 we just need to remove the lines:

if (typeof list['fantasy-land/reduce'] === 'function') {
  return list['fantasy-land/reduce'](fn, acc);
}

in _createReduce.js

and adding in _reduce.js:

var _baseReduce = _createReduce(_arrayReduce, _methodReduce, _iterableReduce);
export default function _reduce(reducer, acc, list) {
  if (typeof list['fantasy-land/reduce'] === 'function') {
    return list['fantasy-land/reduce'](fn, acc);
  }
  return _baseReduce(reducer, acc, list);
}

@xgbuils
Copy link
Contributor Author

xgbuils commented Mar 1, 2022

Hi! Some benchmarks adding benchs for objects in map.bench.js

var obj = {a: 1, b: 2};
...
    'map(sq, obj)': function() {
      map(sq, obj);
    },

Original:

┌────────────────────────┬────────────────────────┬────────────────────────┐
│ map                    │ Hertz                  │ Margin of Error        │
├────────────────────────┼────────────────────────┼────────────────────────┤
│ map(sq, obj)           │ 956,837                │ 4.81%                  │
└────────────────────────┴────────────────────────┴────────────────────────┘

After change:

┌────────────────────────┬────────────────────────┬────────────────────────┐
│ map                    │ Hertz                  │ Margin of Error        │
├────────────────────────┼────────────────────────┼────────────────────────┤
│ map(sq, obj)           │ 2,099,464              │ 6.11%                  │
└────────────────────────┴────────────────────────┴────────────────────────┘

Using more keys implies less difference in gain of performance:

var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643];
var obj = {...nums};
...
    'map(sq, obj)': function() {
      map(sq, obj);
    },

Original:

┌────────────────────────┬────────────────────────┬────────────────────────┐
│ map                    │ Hertz                  │ Margin of Error        │
├────────────────────────┼────────────────────────┼────────────────────────┤
│ map(sq, obj)           │ 571,880                │ 0.41%                  │
└────────────────────────┴────────────────────────┴────────────────────────┘

After change:

┌────────────────────────┬────────────────────────┬────────────────────────┐
│ map                    │ Hertz                  │ Margin of Error        │
├────────────────────────┼────────────────────────┼────────────────────────┤
│ map(sq, obj)           │ 778,917                │ 0.42%                  │
└────────────────────────┴────────────────────────┴────────────────────────┘

CrossEye
CrossEye previously approved these changes Mar 6, 2022
Copy link
Member

@CrossEye CrossEye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me. In the end the code is cleaner, and it's more performant. It's hard to beat that!

🌿

source/ap.js Outdated
function(acc, f) { return _concat(acc, map(f, applyX)); },
[],
applyF
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post 1.0, I would like to do some auto-formatting, using Prettier or something, and from then on simply ignore such things. The trouble, of course, is that auto-formatters are mostly designed around OOP/imperative styles, and those don't always transfer well to FP/declarative code. Still, I would love to simply never have to make such judgement calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. I'm gonna revert this formatting part.

@xgbuils
Copy link
Contributor Author

xgbuils commented Mar 10, 2022

internal _reduce implementation changed and now only works with standard reducers. The new internal _xReduce function only works with transformers. Then, some _reduce usages are hidden and difficult to review because the files didn't change but the implempementation of _reduce did. I'm gonna list the usages of _reduce:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants