Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash partition vs forEach with array push vs two "some" filter loops
(version: 0)
Comparing performance of:
Lodash partition vs forEach vs double filter
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var indexTbl = Array.from({ length: 50 }).map((value, i) => Math.ceil(Math.random()*1000)); var tabl = Array.from({ length: 1000000 }).map((value, i) => i);
Tests:
Lodash partition
let result = _.partition(tabl, v => indexTbl.some(i=>i==v))
forEach
let a = [], b = []; let result = tabl.forEach(item => {if (indexTbl.some(i=>i==item)) {a.push(item)} else {b.push(item)}})
double filter
const a = tabl.filter(item => indexTbl.some(i=>i==item)); const b = tabl.filter(item => !indexTbl.some(i=>i==item));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash partition
forEach
double filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark. **What is being tested?** The test measures the performance of three different approaches to filter an array in JavaScript: 1. Using `lodash.partition` function 2. Using `Array.prototype.forEach` with a custom filtering logic 3. Using two separate `Array.prototype.filter` methods with custom filtering logic These approaches are compared to determine which one is the most efficient. **Options being compared** The options being compared are: * `lodash.partition`: A utility function from the Lodash library that partitions an array into two parts based on a predicate. * `forEach` loop: A traditional JavaScript loop using `Array.prototype.forEach` with a custom filtering logic. * Two separate `filter` methods: Two separate calls to `Array.prototype.filter` with different filtering logic. **Pros and cons of each approach** 1. **Lodash partition**: This approach is concise and elegant, but it requires the use of an external library (Lodash). It also modifies the original array. 2. **forEach loop**: This approach is straightforward and easy to understand, but it can be less efficient due to the overhead of calling `push` on the `Array.prototype`. 3. **Two separate filter methods**: This approach provides more control over the filtering logic, but it requires two separate calls to `filter`, which can lead to unnecessary array creation. **Other considerations** * The use of `Array.from()` to create arrays with random data can introduce additional overhead. * The use of Lodash library may not be desirable for all developers, especially those who prefer to write pure JavaScript code. **Library used** The test uses the Lodash library version 4.17.5, which provides the `partition` function used in one of the test cases. **Special JS feature or syntax** None mentioned in this benchmark. **Benchmark preparation code explanation** The script preparation code creates two arrays: * `indexTbl`: An array with random integers between 0 and 1000. * `tabl`: An array with random integers between 0 and 1,000,000. These arrays are used to simulate a large dataset that will be filtered by the different approaches. **Individual test cases** Each test case measures the performance of one of the three approaches: 1. Lodash partition 2. forEach loop 3. Two separate filter methods The benchmark result shows the average executions per second for each approach on a Chrome 103 browser on a Mac OS X 10.15.7 device. **Alternatives** Other alternatives to these approaches include: * Using `Array.prototype.reduce()` with a custom accumulator function. * Using `Array.prototype.map()` and then filtering the resulting array using `Array.prototype.filter()`. * Using a streaming library like Lodash's `iterate` function or the `stream` API in Node.js.
Related benchmarks:
lodash partition vs forEach with array push vs two filter loops
lodash partition vs forEach with array spread iterator vs array push
lodash partition vs forEach with array spread iterator vs array push vs for each with concat vs two filters
lodash partition vs forEach with array spread iterator vs array push vs for each with concat vs two filters with 10000
Comments
Confirm delete:
Do you really want to delete benchmark?