Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash partition vs ES6 for..of partition
(version: 0)
Comparison of using lodash partition against raw ES6 using for..of loop
Comparing performance of:
_.partition vs ES6 partition with for..of vs ES partition with filter
Created:
2 years ago
by:
Registered User
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 arr = [ { name: 'john', hasPet: true }, { name: 'jack', hasPet: false }, { name: 'jill', hasPet: false }, { name: 'james', hasPet: true }, { name: 'jane', hasPet: false }, ]
Tests:
_.partition
_.partition(arr, el => el.hasPet);
ES6 partition with for..of
const partition = (arr, fun) => { const positive = []; const negative = []; for (el of arr) { if (el.hasPet) { positive.push(el); } else { negative.push(el); } } return [positive, negative]; }; partition(arr, el => el.hasPet);
ES partition with filter
const partition = (arr, fun) => { const positive = arr.filter(fun); const negative = arr.filter(el => !fun(el)); return [positive, negative]; }; partition(arr, el => el.hasPet);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.partition
ES6 partition with for..of
ES partition with 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 world of JavaScript microbenchmarks. **Benchmark Overview** The provided JSON represents a benchmark test case that compares three approaches to partition an array in JavaScript: using Lodash's `partition` function, implementing a custom `partition` function with a for...of loop, and another custom implementation with the Array.prototype.filter() method. **Options Compared** 1. **Lodash `partition`**: This approach uses the built-in `partition` function from the Lodash library. 2. **ES6 Partition with for...of Loop**: This implementation creates two arrays (`positive` and `negative`) using a for...of loop, filtering elements based on the condition provided as an argument. 3. **ES Partition with filter()**: This approach uses Array.prototype.filter() to create two separate arrays: one containing elements that pass the test (positive) and another containing elements that fail (negative). **Pros and Cons** 1. **Lodash `partition`**: * Pros: Built-in function, concise code, easy to read. * Cons: Requires an external dependency (Lodash), may not be optimal for small arrays due to the overhead of the library. 2. **ES6 Partition with for...of Loop**: * Pros: Directly controls execution flow, no external dependencies, can be more efficient for small arrays. * Cons: More verbose code, requires understanding of for...of loops and array iteration. 3. **ES Partition with filter()**: * Pros: Widely supported and well-understood, easy to read, can be more efficient than Lodash's partition function. * Cons: Requires two separate calls to filter(), which might not be as straightforward as a single call. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functions for various tasks, such as array manipulation, object manipulation, and more. In this case, the `partition` function is used to create two arrays from an input array based on a predicate (a test function). **Special JS Feature/Syntax: for...of Loop** The ES6 Partition with for...of Loop implementation uses a modern JavaScript feature called the for...of loop. This syntax allows you to iterate over arrays and other iterable objects in a more concise and expressive way. **Alternative Approaches** Other approaches to partitioning an array might include: 1. Using Array.prototype.every() and Array.prototype.some() to create two separate arrays. 2. Implementing a custom partition function using recursion or other iteration techniques. 3. Utilizing specialized libraries like Ramda or Liskell for functional programming tasks. Keep in mind that the best approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash vs for-of vs forEach
lodash for-in vs native for-in (lodash version: 4.17.10)
lodash vs for-of vs forEach es6
lodash vs for-of vs forEach j
lodash.foreach vs for-of vs array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?