Skip to content

Commit

Permalink
Optimize flatten function for the most common case, when arrs is a JS…
Browse files Browse the repository at this point in the history
… Array.
  • Loading branch information
Oswaldo Ceballos Zavala committed Nov 17, 2017
1 parent 83b0ca4 commit 308578a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ export function isArrayLike(obj) {
}

export function flatten(arrs) {
// optimize for the most common case
if (Array.isArray(arrs)) {
return arrs.reduce(
(flatArrs, item) => flatArrs.concat(isArrayLike(item) ? flatten(item) : item),
[],
);
}

// fallback for arbitrary iterable children
let flatArrs = [];

const iteratorFn = getIteratorFn(arrs);
Expand Down

0 comments on commit 308578a

Please sign in to comment.