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
gemma2:9b
, generated one year ago):
This benchmark compares three different approaches to filtering and sorting an array of objects: **1. `filter` and `sort`:** This approach uses the built-in JavaScript methods `filter` and `sort`. * `filter` creates a new array containing only the elements that pass a given test (in this case, checking if `action` is "BID"). * `sort` then sorts the filtered array based on the `price` property. **2. `reduce`:** This approach uses the `reduce` method to achieve the same result. It iterates over the array and accumulates a new array containing only the elements that meet the criteria. The sorting is implicitly handled within the `reduce` function. Here's a breakdown of the pros and cons: **`filter` and `sort`:** * **Pros:** Clear and concise syntax, leverage built-in methods optimized for performance. * **Cons:** Can be slightly less efficient than `reduce` in some cases, as it creates two separate arrays. **`reduce`:** * **Pros:** More compact and potentially more efficient, as it avoids creating intermediate arrays. * **Cons:** Can be more complex to read and understand for beginners, as the logic is encapsulated within a single function. **Benchmark Results:** The benchmark results show that both approaches are quite fast. The `reduce` method performs slightly better in this particular case, with a higher number of executions per second. However, the difference is not significant. Ultimately, the best approach depends on personal preference and coding style. If you prioritize clarity and readability, `filter` and `sort` might be more suitable. If efficiency is paramount and you're comfortable with more complex logic, `reduce` could be a better choice.
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?