Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Two lambdaless filters vs one lambded v2
(version: 0)
Comparing performance of:
Two filters vs Combined filter
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(5000), () => Math.random()); var more = (n) => n > 0.3; var less = (n) => n < 0.6;
Tests:
Two filters
var filtered = data.filter(more).filter(less);
Combined filter
var filtered = data.filter(n => more(n) && less(n));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Two filters
Combined filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Two filters
5966.8 Ops/sec
Combined filter
1958.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark definition and test cases. **Benchmark Definition:** The website is measuring the performance of different JavaScript approaches for filtering an array of random numbers. The script preparation code defines two functions, `more` and `less`, which take a number as input and return a boolean indicating whether the number is greater than 0.3 or less than 0.6, respectively. **Test Cases:** There are two individual test cases: 1. **"Two filters"`**: This test case uses the traditional callback function approach with multiple filter functions called sequentially. ```javascript var filtered = data.filter(more).filter(less); ``` The `more` and `less` functions are applied one after the other to the filtered array. 2. **"Combined filter"`**: This test case uses a single function expression that combines both filters using logical AND (`&&`) operators. ```javascript var filtered = data.filter(n => more(n) && less(n)); ``` The `more` and `less` functions are embedded inside the arrow function, which is called on each element of the array. **Library:** There is no explicitly mentioned library in the provided benchmark definition. However, it's common to use libraries like Lodash or Ramda for functional programming tasks like filtering arrays. **Pros and Cons:** 1. **"Two filters"`**: - Pros: Easy to understand and implement, especially for those familiar with traditional callback functions. - Cons: Requires multiple function calls, which can be slower due to the overhead of creating new contexts and binding variables. 2. **"Combined filter"`**: - Pros: More concise and readable, as it combines both filters in a single operation. - Cons: May have performance issues if the combined logic is complex or computationally expensive, potentially affecting cache locality and memory access patterns. Other alternatives to consider: * Using `Array.prototype.reduce()` or other aggregation functions might provide alternative approaches to combining filter operations. * Employing more advanced functional programming techniques, such as using `map()`, `reduce()`, or higher-order functions like `forEach()` or `every()`, could potentially yield better performance in certain scenarios. **Special JS Feature/Syntax:** The provided benchmark definition uses arrow function expressions (`n => more(n) && less(n)`), which is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). Arrow functions provide concise syntax for defining small, single-expression functions and can improve readability.
Related benchmarks:
Two lambdaless filters vs one lambded
slice vs filter (10000000)
Array.prototype.some() vs. Filter vs. Array.prototype.indexOf()
slice vs filter for index filtering
Comments
Confirm delete:
Do you really want to delete benchmark?