Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash filter
(version: 2)
Comparing performance of:
Arrow Function Filter vs Prop vs Object vs Filter Prototype vs Explicit Filter Prototype
Created:
8 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
Script Preparation code:
const arr = []; for(let i=0; i < 100; i++){ arr.push({ id: _.random(0, 5) }); } function Arr(){ return arr; }
Tests:
Arrow Function Filter
_.filter(Arr(), (i) => i.id === 5);
Prop
_.filter(Arr(), 'id', 5);
Object
_.filter(Arr(), {id: 5});
Filter Prototype
Arr().filter((i) => i.id === 5);
Explicit Filter Prototype
Array.prototype.filter.call(Arr(), (i) => i.id === 5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Arrow Function Filter
Prop
Object
Filter Prototype
Explicit Filter Prototype
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 break down the provided benchmark and its test cases to understand what's being tested. **Benchmark Definition** The benchmark is based on the Lodash library, which provides functional programming utilities in JavaScript. The benchmark measures the performance of the `_.filter()` function with different approaches for filtering an array of objects. **Script Preparation Code** The script preparation code creates a JavaScript array `arr` with 100 elements, each having a random `id` property between 0 and 5. This array will be used as input for the benchmark tests. **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library from a CDN. **Test Cases** There are five test cases: 1. **Arrow Function Filter**: Uses an arrow function `(i) => i.id === 5` to filter the array. 2. **Prop**: Uses the `_.filter()` method with a string argument `'id'` and a value of 5 to filter the array. 3. **Object**: Uses the `_.filter()` method with an object `{id: 5}` as the filter criterion to filter the array. 4. **Filter Prototype**: Uses the `Arr().filter()` method (i.e., calling `Array.prototype.filter()` on a custom array `Arr()`) to filter the array. 5. **Explicit Filter Prototype**: Uses the `Array.prototype.filter.call(Arr(), ...)` syntax to explicitly call the `Array.prototype.filter()` method on the `Arr()` array. **What's being tested?** Each test case measures the performance of the corresponding filtering approach: * The `_.filter()` method with an arrow function, string, or object as the filter criterion. * The custom `Arr().filter()` method and its equivalent using `Array.prototype.filter.call()`. * The built-in `Array.prototype.filter()` method. **Pros and Cons** Here's a brief summary of each approach: 1. **Arrow Function Filter**: Pros: concise, easy to read; Cons: may not be as efficient due to the creation of an intermediate scope. 2. **Prop**: Pros: simple, straightforward; Cons: may not work well with more complex filter criteria. 3. **Object**: Pros: flexible, can handle various filter criteria; Cons: may require additional setup or checks for object properties. 4. **Filter Prototype**: Pros: concise, uses a standard method; Cons: requires creating an array instance `Arr()` specifically for filtering. 5. **Explicit Filter Prototype**: Pros: explicit, easy to understand; Cons: verbose and may not be as efficient. **Library and Purpose** Lodash is a popular JavaScript library that provides functional programming utilities, including the `_.filter()` method. The purpose of this benchmark is to compare the performance of different filtering approaches and provide insights for developers who need to optimize their code. **Special JS Features/Syntax (not applicable in this case)** No special JavaScript features or syntax are used in these test cases.
Related benchmarks:
Lodash.filter vs array.filter
lodash / native filter with includes
Lodash filter 1 000 000
Unique lodash vs vanilla
Comments
Confirm delete:
Do you really want to delete benchmark?