Last active
July 25, 2016 10:52
-
-
Save ro-savage/9ca7342ed87f53002b007a500febad0f to your computer and use it in GitHub Desktop.
Methods for filtering a large array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const t0 = performance.now() | |
// slowest | |
const results = list.filter((item) => { | |
return matchObjPropsToValue(item, searchTerm, props, fullTextSearch) | |
}) | |
Alternative method using currying an ramda. | |
// fast | |
import { matchObjPropsToValue, mapWhile } from '../helpers/match-helpers' | |
const results = filterWhile( | |
newList => newList.length <= 10, | |
item => matchObjPropsToValue(item, searchTerm, props, fullTextSearch) === true ? item : false, | |
list | |
) | |
// fast | |
const len = list.length | |
const results = [] | |
for (let i = 0; i < len; i++) { | |
if (results.length === limit) {break} | |
const item = list[i] | |
matchObjPropsToValue(item, searchTerm, props, fullTextSearch) && results.push(item) | |
} | |
//fastest | |
const results = [] | |
list.some(item => { | |
matchObjPropsToValue(item, searchTerm, props, fullTextSearch) ? results.push(item) : null | |
return results.length === limit | |
}) | |
const t1 = performance.now() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requires ramda | |
import rCurry from 'ramda/src/curry' | |
import rReduce from 'ramda/src/reduce' | |
import rReduced from 'ramda/src/reduced' | |
export const filterWhile = rCurry(( | |
predicate, // while fn - receives current partial list & value, returns bool | |
transformation, // map fn | |
list // source list | |
) => rReduce((oldAcc, oldVal) => { | |
const newVal = transformation(oldVal) | |
const newAcc = newVal === false ? oldAcc : [...oldAcc, newVal] | |
return predicate(newAcc, newVal) ? newAcc : rReduced(oldAcc) | |
}, [], list)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Performance results | |
JS Filter: | |
Entire array: 29.869 | 26.094 | 29.770 | |
Partial: 24.294 | 42.849 | 24.229 | |
None: 14.819 | 6.959 | 7.434 | |
filterWhile: | |
Entire array: 30.229 | 25.079 | 25.770 | |
Partial: 12.879 | 8.130 | 8.0449 | |
None: 0.885 | 0.059 | 0.100 | |
for loop: | |
(Approx same as filterWhile) | |
[].some: | |
Entire array: 28.324 | 27.025 | 19.895 | |
Partial: 11.969 | 9.880 | 7.369 | |
None: 0.009 | 0.005 | 0.005 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment