Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sort-filter vs reduce
(version: 0)
Comparing performance of:
sort-filter vs reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// filterFoo = (x) => x.action === "BID" && x.symbol.toLocaleLowerCase().includes('btc') sortFoo = (a, b) => { return a.price - b.price } reduceFoo = (value, item) => { if ( !( item.action === "BID" && item.symbol.toLocaleLowerCase().includes('btc') ) ) 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" }]
Tests:
sort-filter
const sorted = arr.filter(filterFoo).sort(sortFoo) const bigPrice = sorted[0].price
reduce
const bigPrice = arr.reduce(reduceFoo, 0)
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sort-filter
2700134.2 Ops/sec
reduce
4923494.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The benchmark is comparing three approaches for filtering and sorting an array: 1. `filter` + `sort` 2. `reduce` The benchmark definition consists of two individual test cases: * "sort-filter" tests the combination of `filter` and `sort` methods. * "reduce" tests the use of the `reduce` method to achieve a similar result. **Options Compared** Here's what each option does: 1. **Filter + Sort**: This approach uses the `filter` method to remove unwanted elements from the array, followed by the `sort` method to arrange the remaining elements in ascending order. 2. **Reduce**: This approach uses the `reduce` method to iterate over the array and accumulate a value that represents the maximum price among all elements. **Pros and Cons** Here's a brief summary of each option: * **Filter + Sort**: + Pros: Easy to understand, widely supported by most browsers. + Cons: Can be slower than `reduce` due to the overhead of sorting. * **Reduce**: + Pros: Often faster than `filter + sort`, as it avoids the overhead of sorting. + Cons: Requires understanding of the accumulator function and can be less intuitive for some developers. **Library/Functionality** The benchmark uses the following JavaScript functionality: * **`filter` method**: Removes unwanted elements from an array based on a provided predicate function. * **`sort` method**: Sorts the elements of an array in place based on their values. * **`reduce` method**: Applies a reduce callback function to each element of an array, accumulating a value. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's already mentioned (e.g., arrow functions). **Alternatives** If you're interested in exploring alternative approaches, here are some options: * **Using `Array.prototype.map` and then `Math.max`**: This approach can be similar to `reduce`, but uses the `map` method instead. * **Using a custom iterative solution**: You could write a custom loop to iterate over the array and accumulate the maximum price, avoiding the use of built-in methods like `filter`, `sort`, or `reduce`. Keep in mind that these alternatives might have different performance characteristics and may not be as efficient as the original approaches.
Related benchmarks:
filter + map vs reduce
filter.map vs reduce 2
sort vs reduce for a few elements
sort vs reduce v2
Reduce vs map with empty filter
Comments
Confirm delete:
Do you really want to delete benchmark?