Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Simple filter sort vs reduce
(version: 0)
Comparing performance of:
sort filter vs reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
sort filter
// filterFoo = (x) => x.action === "BID"; sortFoo = (a, b) => a.price - b.price; reduceFoo = (value, item) => { if (!(item.action === "BID")) return value; return value >= item?.price ? value : item?.price; } // arr = [{ price: 1000000000, action: "ASK", symbol: "BTC_IRT" }, { price: 1000000, action: "ASK", symbol: "BTC_IRT" }, { price: 111111, action: "BID", symbol: "BTC_IRT" }, { price: 100000, action: "BID", symbol: "BTC_IRT" }, { price: 11111, action: "BID", symbol: "BTC_IRT" }, { price: 10000, action: "BID", symbol: "BTC_IRT" }, { price: 1111, action: "BID", symbol: "BTC_IRT" }, { price: 1000, action: "BID", symbol: "BTC_IRT" }, { price: 100, action: "BID", symbol: "BTC_IRT" }, { price: 1, action: "BID", symbol: "BTC_IRT" }, { price: 11111, action: "ASK", symbol: "BTC_USDT" }, { price: 25000, action: "ASK", symbol: "USDT_IRT" }, { price: 11111, action: "ASK", symbol: "USDT_IRT" }, { price: 10000, action: "ASK", symbol: "USDT_IRT" }, { price: 1111, action: "BID", symbol: "USDT_IRT" }] arr.filter(filterFoo).sort(sortFoo);
reduce
// filterFoo = (x) => x.action === "BID"; sortFoo = (a, b) => a.price - b.price; reduceFoo = (value, item) => { if (!(item.action === "BID")) return value; return value >= item?.price ? value : item?.price; } // arr = [{ price: 1000000000, action: "ASK", symbol: "BTC_IRT" }, { price: 1000000, action: "ASK", symbol: "BTC_IRT" }, { price: 111111, action: "BID", symbol: "BTC_IRT" }, { price: 100000, action: "BID", symbol: "BTC_IRT" }, { price: 11111, action: "BID", symbol: "BTC_IRT" }, { price: 10000, action: "BID", symbol: "BTC_IRT" }, { price: 1111, action: "BID", symbol: "BTC_IRT" }, { price: 1000, action: "BID", symbol: "BTC_IRT" }, { price: 100, action: "BID", symbol: "BTC_IRT" }, { price: 1, action: "BID", symbol: "BTC_IRT" }, { price: 11111, action: "ASK", symbol: "BTC_USDT" }, { price: 25000, action: "ASK", symbol: "USDT_IRT" }, { price: 11111, action: "ASK", symbol: "USDT_IRT" }, { price: 10000, action: "ASK", symbol: "USDT_IRT" }, { price: 1111, action: "BID", symbol: "USDT_IRT" }] arr.reduce(reduceFoo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sort filter
reduce
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.1:latest
, generated one year ago):
Let's dive into the benchmark definition and results. **Benchmark Description** The benchmark tests the performance of three different approaches to filtering and sorting an array of objects: 1. **Sort-Filter**: This approach first filters out unwanted elements using the `filter()` method, and then sorts the remaining elements using the `sort()` method. 2. **Reduce**: This approach uses the `reduce()` method to accumulate a single value from the array, while filtering out unwanted elements. **Test Cases** The benchmark has two test cases: 1. **Sort-Filter**: The code first defines two functions, `filterFoo` and `sortFoo`, which filter out elements with action "BID" and sort them in ascending order of price, respectively. Then it calls the `filter()` method on an array of objects using `filterFoo`, followed by the `sort()` method using `sortFoo`. 2. **Reduce**: The code defines the same two functions as above, but instead of calling `filter()` and `sort()`, it uses the `reduce()` method to accumulate a single value from the array while filtering out unwanted elements. **Library** No external library is used in this benchmark. **Special JS Feature or Syntax** None mentioned in this specific benchmark. However, note that JavaScript features like async/await, Promises, and Arrow Functions are not used here. **Pros/Cons of Different Approaches** * **Sort-Filter**: This approach has the advantage of being a standard way to filter and sort arrays in JavaScript. It's easy to understand and implement. However, it may be less efficient than other methods when dealing with large datasets or complex filtering logic. * **Reduce**: The reduce method is designed for accumulating values from an array. In this case, it's used to accumulate the maximum price of "BID" elements while filtering out unwanted ones. This approach can be more efficient and concise than Sort-Filter, especially when dealing with large datasets. **Other Considerations** * **Array Size**: The benchmark uses a relatively small array size (12 elements). For larger arrays or complex filtering logic, the performance difference between Sort-Filter and Reduce may become more significant. * **Browser Performance**: As seen in the latest benchmark results, different browsers can exhibit varying performance characteristics. In this case, Mobile Safari 15 is faster than Mobile Safari 15 when running the reduce method. **Conclusion** This benchmark highlights two popular approaches to filtering and sorting arrays in JavaScript: Sort-Filter and Reduce. While both methods have their advantages and disadvantages, the choice of approach depends on specific use cases, array size, and browser performance characteristics.
Related benchmarks:
sort vs reduce
sort vs reduce: small set
sort vs reduce for a few elements
sort vs reduce v2
sort vs reduce v3
Comments
Confirm delete:
Do you really want to delete benchmark?