{"ScriptPreparationCode":"function getRandomIntInclusive(min, max) {\r\n min = Math.ceil(min);\r\n max = Math.floor(max);\r\n return Math.floor(Math.random() * (max - min \u002B 1)) \u002B min; //The maximum is inclusive and the minimum is inclusive \r\n}\r\n\r\nvar arrayOfRandomInts = Array.from({\r\n length: 2500\r\n}, () =\u003E getRandomIntInclusive(1, 10));\r\n\r\nfunction isValid(e) {\r\n for (let i = 0; i \u003C 1000; i\u002B\u002B) {\r\n // Fake workload to simulate a \u0022heavy\u0022 predicate\r\n }\r\n\r\n return e \u003E 5;\r\n}\r\n\r\nfunction partitionTwoFilters(array, predicate) {\r\n return [\r\n array.filter((i) =\u003E predicate(i)),\r\n array.filter((i) =\u003E !predicate(i)),\r\n ];\r\n}\r\n\r\nfunction partitionForEach(array, predicate) {\r\n const pass = [];\r\n const fail = [];\r\n array.forEach(elem =\u003E {\r\n predicate(elem) ? pass.push(elem) : fail.push(elem);\r\n });\r\n return [pass, fail];\r\n}\r\n\r\nfunction partitionForOf(array, predicate) {\r\n const result = [];\r\n const rest = [];\r\n for (const value of array) {\r\n if (predicate(value, result.length, result)) {\r\n result.push(value);\r\n } else {\r\n rest.push(value);\r\n }\r\n }\r\n return [result, rest];\r\n}\r\n\r\nfunction partitionReduceSpread(array, predicate) {\r\n return array.reduce(([pass, fail], elem) =\u003E {\r\n return predicate(elem) ? [\r\n [...pass, elem], fail\r\n ] : [pass, [...fail, elem]];\r\n }, [\r\n [],\r\n []\r\n ]);\r\n}\r\n\r\n// https://codereview.stackexchange.com/a/162879\r\nfunction partitionReducePush(array, predicate) {\r\n return array.reduce(\r\n (result, element) =\u003E {\r\n result[predicate(element) ? 0 : 1].push(element);\r\n return result;\r\n },\r\n [[], []],\r\n );\r\n}\r\n","TestCases":[{"Name":"Two filters","Code":"const [pass, fail] = partitionTwoFilters(arrayOfRandomInts, isValid);","IsDeferred":false},{"Name":"forEach","Code":"const [pass, fail] = partitionForEach(arrayOfRandomInts, isValid);","IsDeferred":false},{"Name":"for of","Code":"const [pass, fail] = partitionForOf(arrayOfRandomInts, isValid);","IsDeferred":false},{"Name":"Reduce \u002B Spread","Code":"const [pass, fail] = partitionReduceSpread(arrayOfRandomInts, isValid);","IsDeferred":false},{"Name":"reduce push","Code":"const [pass, fail] = partitionReducePush(arrayOfRandomInts, isValid);","IsDeferred":false}]}